
Arun
Jan 07, 2025
Arrays and Strings Explained with Examples
Understand Arrays and Strings from the ground up with simple explanations, visual thinking, and real coding examples for DSA beginners.
Arrays and Strings are the foundation of Data Structures and Algorithms. Almost every problem you solve in DSA will involve one of these two in some way, which is why understanding them clearly is extremely important.
What is an Array?
An array is a collection of elements stored at continuous memory locations. This means each element can be accessed directly using its index, making arrays very fast for read operations.
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[2]); // Output: 30In the example above, each value is stored next to each other in memory. Accessing any element takes constant time, which is why arrays are efficient for lookups.
Common Array Operations
The most common operations performed on arrays include traversal, insertion, deletion, and searching. Traversal means visiting each element one by one.
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}Insertion and deletion in arrays can be costly because elements may need to be shifted, especially when working with fixed-size arrays.
What is a String?
A string is essentially an array of characters. In most programming languages, strings are immutable, meaning once created, they cannot be changed.
String text = "Hello";
System.out.println(text.charAt(1)); // Output: eEven though strings feel simple, many interview problems are based on string manipulation such as reversing, checking palindromes, or counting character frequency.
Common String Problems in DSA
Some very common string problems include reversing a string, finding the longest substring, checking for anagrams, and removing duplicate characters.
String str = "hello";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(reversed); // Output: ollehUsing tools like StringBuilder helps improve performance, especially when working with large strings.
Why Arrays and Strings Matter in Interviews
Interviewers often start with array and string problems because they test your problem-solving ability, logic, and understanding of time and space complexity.
Once you are comfortable with arrays and strings, learning advanced topics like sliding window, two pointers, and hashing becomes much easier.