Array & String Algorithms

Array and String Algorithm Patterns for Interviews
Array and string problems are the most common category in coding interviews, appearing in approximately 35% of all questions. Mastering 5 core patterns will equip you to solve the majority of these problems.
The five essential array/string patterns are: Two Pointers, Sliding Window, Prefix Sum, Sorting + Binary Search, and Hash Map Frequency Counting.
Pattern 1: Two Pointers
Use when: sorted array, finding pairs, partitioning. Example: "Find two numbers that sum to target in a sorted array." Start left and right pointers at array boundaries, move based on comparison. O(n) time, O(1) space.
Pattern 2: Sliding Window
Use when: contiguous subarray/substring, maximum/minimum in a range. Example: "Find the longest substring without repeating characters." Maintain a window with two pointers. O(n) time, O(k) space where k is window size.
Pattern 3: Prefix Sum
Use when: range sum queries, subarray sum equals target. Precompute prefix sums to answer range queries in O(1). Total setup is O(n).
Pattern 4: Binary Search on Sorted Arrays
Use when: sorted data, finding boundaries. See our complete binary search guide for advanced variations like search in rotated array.
Pattern 5: Hash Map Frequency
Use when: counting occurrences, finding duplicates, anagram detection. O(n) time, O(n) space. See hash tables interview guide.
Practice these patterns with real-time AI hints in AissenceAI's coding practice mode.