TechHubCode LogoTechHubCode
🔍
Arun Bhardwaj

Arun Bhardwaj

07 Jan 2025

Arrays and Strings Explained with Examples

8 min readBeginner
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 allows direct access using indexes, making arrays very fast for read operations.

int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[2]); // Output: 30

In the example above, each value is stored next to each other in memory. Accessing any element takes constant time.

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 can be expensive because elements may need to be shifted, especially in fixed-size arrays.

What is a String?

A string is essentially an array of characters. In most programming languages, strings are immutable, meaning they cannot be changed once created.

String text = "Hello";
System.out.println(text.charAt(1)); // Output: e

Despite appearing simple, many interview problems are based on string manipulation such as reversing strings or checking palindromes.

Common String Problems in DSA

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: olleh

Using StringBuilder improves performance when working with large strings.

Why Arrays and Strings Matter in Interviews

Interviewers often start with array and string problems because they test problem-solving skills, logical thinking, and understanding of time and space complexity.

Once you master arrays and strings, advanced concepts like sliding window, two pointers, and hashing become much easier to learn.