Arrays and Strings — The Complete FAANG Interview Pattern Guide
Advertisement
Problem Statement
Arrays and strings dominate FAANG screening rounds. Roughly 35% of coding interviews open with an array/string problem because they reveal indexing discipline, boundary handling, and pattern recognition in 30 minutes.
Constraints (typical interview ranges):
- 1 <= n <= 10^5 array length
- 1 <= s.length <= 10^5 string length
- Values often in 32-bit integer range
- ASCII or lowercase English letters
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]Input: s = "leetcode"
Output: 'l' is the first unique characterWhy This Problem Matters
The array interview question is the gateway test for every FAANG candidate. Amazon, Google, Meta, Apple, and Microsoft all use them in phone screens and onsite loops because arrays expose how a candidate translates intent into code without crutches like recursion or framework magic. If you can manipulate indices cleanly, you can do almost anything else.
Strings are arrays in disguise. Every string FAANG question reduces to two ideas: indexing into characters and tracking state with hash maps or counters. Mastery of array and string patterns transfers directly to trees (DFS uses arrays as paths), dynamic programming (DP tables are arrays), and graphs (adjacency lists are arrays of arrays).
This guide covers the seven patterns that solve more than 87% of array and string LeetCode problems asked at top tech companies.
The Core Insight
The trick is pattern recognition, not memorization. Every array/string problem is one of: prefix-sum reduction, Kadane running maximum, two pointers converging or chasing, a sliding window expanding and contracting, a hash map counting frequencies, sorting then sweeping, or Dutch-flag in-place partitioning. Pick the right pattern and the implementation falls out in 15 lines.
A second insight: in-place beats copy. Interviewers grade O(1) extra space heavily because it shows you understand pointer arithmetic. Whenever you reach for a new array, ask whether you can swap, mark, or reverse instead.
The third insight: state the brute force first. Naming the O(n^2) solution before writing the O(n) one is what separates a hire from a no-hire. The bottleneck-and-optimize narrative is the FAANG signal.
Visual Dry Run
| Pattern | When to Use | Typical Complexity |
|---|---|---|
| Prefix Sum | Range queries, subarray sums | O(n) preprocess |
| Kadane | Maximum subarray | O(n) one pass |
| Two Pointers | Pair sums, palindromes | O(n) |
| Sliding Window | Longest/shortest substring | O(n) |
| Hash Map | Frequency, complement | O(n) |
| Sort and Sweep | Intervals, anagrams | O(n log n) |
| Dutch Flag | 3-way partition | O(n) one pass |
Solution (Optimal)
The Two Sum hash map below shows the canonical pattern: trade space for time, scan once, look up complements.
class Solution:
def two_sum(self, nums, target):
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []var twoSum = function(nums, target) {
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
}
seen.set(nums[i], i);
}
return [];
};Time: O(n) — single pass over the array, hash lookup is O(1) average. Space: O(n) — hash map can hold up to n entries.
Kadane in one line:
class Solution:
def max_subarray(self, nums):
best = curr = nums[0]
for n in nums[1:]:
curr = max(n, curr + n)
best = max(best, curr)
return bestvar maxSubArray = function(nums) {
let best = nums[0], curr = nums[0];
for (let i = 1; i < nums.length; i++) {
curr = Math.max(nums[i], curr + nums[i]);
best = Math.max(best, curr);
}
return best;
};Time: O(n) — one scan. Space: O(1) — two scalar variables.
Common Mistakes
- Using O(n^2) brute force when interviewer expects optimization narrative.
- Forgetting integer overflow when summing prefixes in Java or C++.
- Off-by-one errors in sliding window when shrinking from the left.
- Mutating the input array silently when interviewer wanted no side effects.
- Treating strings as immutable in Python without converting to a list.
Interview Tips
- Always state brute force, then bottleneck, then optimization.
- Confirm constraints first — empty arrays, single elements, negatives.
- Draw the array on paper and step through indices for the first three iterations.
- Pre-allocate the answer array if you know its size to avoid resize cost.
- Use Python dict.get(x, 0) or JavaScript Map for clean frequency counting.
Follow-up Questions
- Can you solve this with O(1) extra space? Hint: in-place swap.
- What if the array is sorted? Hint: two pointers replace the hash map.
- What if duplicates exist? Hint: store all indices for each value.
- Can you stream the data? Hint: maintain running aggregates.
- What if the array is too large for memory? Hint: external sort or sketches.
Key Takeaways
- 7 core patterns solve 87% of LeetCode array and string problems.
- Two pointers and sliding window dominate strings; prefix sum and Kadane dominate arrays.
- Always describe brute force before optimal — that narrative is the FAANG signal.
- In-place solutions score higher than copy-based ones at every FAANG.
- Hash maps trade O(n) space for O(n) time and unlock most pair-sum problems.
- Sorting first is a legitimate first move for interval and anagram problems.
- Pattern recognition beats rote memorization for long-term interview success.
Advertisement