Zero To DSAZero To DSA
Privacy Policy
Learning Path
Big O & ComplexityArrays & StringsHashMaps & SetsLinked ListsStacks & QueuesRecursion & BacktrackingSorting & SearchingGreedy & IntervalsTrees & TriesGraphsDynamic Programming

Arrays & Strings

beginner5 problems· Prerequisites: Big O & Complexity
Learn
Practice
Exam

Array Basics

Arrays store elements in contiguous memory with O(1) index-based access. Arrays are fixed-size. Use a dynamic array for dynamic sizing.

| Operation | Array | Dynamic Array | |---|---|---|---| | Access by index | O(1) | O(1) | | Search (unsorted) | O(n) | O(n) | | Insert at end | N/A (fixed) | O(1) amortized | | Insert at middle | O(n) (shift) | O(n) | | Remove at end | N/A | O(1) | | Remove at middle | O(n) | O(n) |

Array vs List declarationsC#
// Fixed-size array
int[] arr = new int[5] { 1, 2, 3, 4, 5 };

// Dynamic list
List<int> list = new List<int> { 1, 2, 3 };
list.Add(4);                    // O(1) amortized
list.Insert(0, 0);              // O(n) — shifts elements
list.RemoveAt(1);               // O(n) — shifts elements

// Array utilities
Array.Sort(arr);                // O(n log n)
Array.Reverse(arr);             // O(n)
int index = Array.IndexOf(arr, 3); // O(n)

Two-Pointer Technique

Two pointers iterate from different positions toward each other (or in the same direction at different speeds). Useful for sorted arrays, palindromes, and in-place reversal.

Common patterns:

  1. Opposite ends — one at start, one at end, move toward each other
  2. Same direction — slow and fast pointer (also used in linked lists)
  3. Sliding window — maintain a window that expands/contracts
Two pointers from opposite endsC#
// Reverse an array in-place — O(n), O(1) space
void Reverse(int[] arr) {
    int left = 0, right = arr.Length - 1;
    while (left < right) {
        (arr[left], arr[right]) = (arr[right], arr[left]);
        left++;
        right--;
    }
}

// Check if a string is a palindrome — O(n), O(1) space
bool IsPalindrome(string s) {
    int i = 0, j = s.Length - 1;
    while (i < j) {
        if (s[i] != s[j]) return false;
        i++; j--;
    }
    return true;
}

Sliding Window

A window (subarray) slides across the array. Used for subarray problems like max sum subarray of size k or longest substring without repeating characters.

Fixed window: Window size is constant. Variable window: Window expands and contracts based on a condition.

Complexity: O(n) — each element enters and leaves the window at most once.

Fixed-size sliding windowC#
// Max sum of any subarray of size k — O(n)
int MaxSumSubarray(int[] arr, int k) {
    int windowSum = 0;
    for (int i = 0; i < k; i++)
        windowSum += arr[i];

    int maxSum = windowSum;
    for (int i = k; i < arr.Length; i++) {
        windowSum += arr[i] - arr[i - k];
        maxSum = Math.Max(maxSum, windowSum);
    }
    return maxSum;
}

Common Interview Patterns

  1. Two Sum variant — use a hash map for O(n) lookup
  2. In-place array manipulation — maintain a "write index"
  3. Prefix sum — precompute cumulative sums for O(1) range sum queries
  4. String builder — anytime you need to build/modify a string in a loop
  5. Character counting — use int[26] for lowercase letters, Dictionary<char,int> for general