Arrays & Strings – DSA Concepts, Examples & MCQs (2025)

Arrays & Strings: Complete Guide for DSA Preparation (2025)

Arrays and Strings are the foundation of Data Structures & Algorithms (DSA) and play a key role in coding interviews, aptitude tests, and competitive programming. Mastering these concepts helps you solve problems in searching, sorting, pattern matching, and memory management.

What is an Array?

  • An Array is a collection of elements stored in contiguous memory locations.
  • Each element can be accessed using an index (starting from 0).
  • Useful when we need to store multiple values of the same type.

Example in C++:

 
int arr[5] = {10, 20, 30, 40, 50}; cout << arr[2]; // Output: 30

Key Features:

  • Fixed size.
  • Random access (O(1)).
  • Efficient traversal.

What is a String?

  • A String is a sequence of characters stored as an array of characters.
  • In most programming languages, Strings are treated as a data type.

Example in Java:

 
String s = "Hello World"; System.out.println(s.charAt(0)); // Output: H

Key Features:

  • Strings are arrays of characters.
  • Can perform operations like concatenation, substring, search.
  • Commonly used in text processing, pattern matching, parsing.

Common Operations

Arrays

  • Traversal → Print all elements.
  • Insertion & Deletion.
  • Searching (Linear & Binary Search).
  • Sorting (Bubble, Quick, Merge).

Strings

  • Length of string.
  • Reverse a string.
  • Palindrome check.
  • Substring & pattern search.

Solved Examples

Example 1 (Array):
Find the sum of elements in array {1, 2, 3, 4, 5}.
Sum = 1 + 2 + 3 + 4 + 5 = 15

Example 2 (String):
Check if "level" is a palindrome.
"level" reversed is "level"Yes, it’s a palindrome.

Practice Questions

  1. In C, array indexing starts from:
    (a) 0
    (b) 1
    (c) -1
    (d) None

  2. Which operation on an array takes O(1) time?
    (a) Searching
    (b) Traversal
    (c) Access by index
    (d) Sorting

  3. In Java, which method returns the length of a string?
    (a) size()
    (b) length()
    (c) getLength()
    (d) strLength()

  4. Which of the following is a palindrome string?
    (a) hello
    (b) racecar
    (c) apple
    (d) java

Tips for Exam Preparation

  • Practice array problems: reverse, rotate, find max/min, subarray sum.
  • Revise string problems: palindrome, anagram, substring search.
  • Learn time complexities of array/string operations.
  • Attempt coding exercises on LeetCode, HackerRank, GFG.

FAQs:

Q1. What is the difference between Arrays and Strings?
Arrays store elements of the same type, Strings are a collection of characters (special type of array).

Q2. Why are arrays faster than other data structures?
Because they provide direct access (O(1)) using index.

Q3. Can strings be modified in Java?
No, Strings are immutable in Java. For modifications, use StringBuilder.

Q4. Which is better for text processing: Arrays or Strings?
Strings are more convenient, but arrays may be faster for performance-critical tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *