Coding

Arrays & Strings Coding Questions

Arrays and strings are the foundation of coding interviews. These problems appear at every company and test your ability to work with indices, pointers, and in-place manipulation efficiently.

Sample Questions & Strong Answers

1

Two Sum — find two numbers in an array that add up to a target.

Use a hash map: iterate through the array, storing each number's index. For each element x, check if target − x exists in the map. Time O(n), Space O(n). Brute force is O(n²). Follow-up: if sorted, use two pointers for O(1) space.

💡

Pro tip: Always ask if the array is sorted — that unlocks the two-pointer O(n) O(1) solution. Asked by Google, Amazon, Meta.

2

Best Time to Buy and Sell Stock — maximize profit from one buy/sell.

Track the minimum price seen so far as you iterate. At each day, compute profit = price − minPrice and update maxProfit. Time O(n), Space O(1). Never use nested loops — that's O(n²).

💡

Pro tip: The key insight: you buy at the global minimum before the day you sell, not necessarily at the absolute min. Classic Amazon / Apple question.

3

Maximum Subarray (Kadane's Algorithm).

Maintain currentSum (best subarray ending here) and maxSum. At each index: currentSum = max(nums[i], currentSum + nums[i]). Update maxSum = max(maxSum, currentSum). Time O(n), Space O(1).

💡

Pro tip: The trick: when currentSum goes negative, reset it to 0 (start a new subarray). Frequently asked at Amazon, Google.

4

Product of Array Except Self — no division allowed.

Two passes: left pass stores product of all elements to the left of i. Right pass multiplies product of all elements to the right. Final answer array = left[i] × right[i]. Time O(n), Space O(1) (output array doesn't count).

💡

Pro tip: The 'no division' constraint forces the two-pass approach. Know why division fails for zeros. Meta, Amazon, Microsoft favorite.

5

Container With Most Water — find two lines that hold the most water.

Two pointers at each end. Area = min(height[l], height[r]) × (r − l). Move the pointer with the smaller height inward. Time O(n), Space O(1). Greedy: always move the shorter side to potentially find a taller one.

💡

Pro tip: The insight is counterintuitive: moving the taller pointer can only decrease area, so always move the shorter one. Google, Meta, Amazon.

6

3Sum — find all unique triplets summing to zero.

Sort the array. Fix one element, use two pointers on the rest. Skip duplicates at every level. Time O(n²), Space O(1) (excluding output). Critical: deduplicate by skipping equal adjacent elements at the fixed pointer, left pointer, and right pointer.

💡

Pro tip: Sorting first makes deduplication clean. The O(n²) two-pointer approach is optimal. Google, Meta, Microsoft.

7

Trapping Rain Water — how much water can be trapped between bars.

For each position, water = min(maxLeft, maxRight) − height[i]. Precompute left/right max arrays (O(n) time, O(n) space), or use two pointers for O(1) space: the pointer with the smaller max governs the water at that position.

💡

Pro tip: The two-pointer solution is elegant but hard to derive. Explain both solutions and why two-pointer is correct. Amazon, Google, Meta.

8

Longest Substring Without Repeating Characters.

Sliding window with a hash map tracking last seen index of each character. Move left pointer to max(left, lastSeen[char] + 1) when a repeat is found. Update max length on each step. Time O(n), Space O(1) (bounded by character set).

💡

Pro tip: The subtle point: left = max(left, map[char] + 1) — don't let left move backward if the repeat was before the window. Amazon, Meta, Microsoft.

9

Minimum Window Substring — smallest window containing all chars of t.

Sliding window: expand right until window contains all chars, then shrink from left while valid. Track counts with a hash map and a 'formed' counter. Time O(s + t), Space O(s + t). Return the minimum valid window found.

💡

Pro tip: The 'formed' counter (tracking how many unique chars meet their required count) avoids O(k) window validity checks. Meta, Google — hard difficulty.

10

Group Anagrams — group strings that are anagrams of each other.

For each string, sort its characters to create a canonical key. Store in a hash map: key → list of anagrams. Return the map values. Time O(n × k log k) where k is max string length. Alternative key: character frequency array tuple.

💡

Pro tip: Sorted character string as a key is the canonical approach. Amazon, Meta, Google — commonly used as a warm-up.

11

Find Minimum in Rotated Sorted Array.

Binary search: if nums[mid] > nums[right], the minimum is in the right half. Otherwise, it's in the left half (including mid). Converges when left === right. Time O(log n), Space O(1).

💡

Pro tip: Compare mid to right (not left) — this correctly handles the non-rotated case. Microsoft, Amazon standard question.

12

Search in Rotated Sorted Array.

Binary search: determine which half is sorted by comparing nums[left] and nums[mid]. If the target falls within the sorted half, search there. Otherwise, search the other half. Time O(log n).

💡

Pro tip: First identify which half is sorted, then check if target lies there — two conditions per step. Google, Microsoft.

13

Rotate Array — rotate array right by k steps.

Reverse the entire array, then reverse first k elements, then reverse remaining. Three reversal trick: O(n) time, O(1) space. Alternative: use extra array (O(n) space). k = k % n to handle k > n.

💡

Pro tip: The three-reversal trick is elegant and expected. Extra array approach is fine for explanation but mention the optimal. Microsoft, Apple, Amazon.

14

Valid Anagram — check if two strings are anagrams.

Count character frequencies: use a 26-element array (lowercase letters) or hash map. Increment for s, decrement for t. If all zeros at end, they're anagrams. Time O(n), Space O(1). Shorter: sort both and compare.

💡

Pro tip: Frequency array is faster than sorting (O(n) vs O(n log n)). Always clarify: unicode or ASCII only? Amazon, Meta, Apple.

15

Merge Sorted Arrays (in-place) — merge nums2 into nums1.

Fill from the back: compare largest elements of both arrays, place larger at end of nums1. Use three pointers: i = m−1, j = n−1, k = m+n−1. Time O(m+n), Space O(1). Starting from back avoids overwriting unprocessed elements.

💡

Pro tip: Filling from the back is the key insight — avoids the O(n) shift problem of front-to-back merging. Amazon, Microsoft, Apple.

Expert tips

1

Master two pointers and sliding window — they solve the majority of array/string problems in O(n).

2

Hash maps convert O(n²) brute force to O(n) for lookup-heavy problems — always consider them first.

3

Sort first if the problem involves finding pairs or groups — sorting enables two-pointer and deduplication.

4

Practice in-place manipulation (reversals, rotations) — they test pointer/index control without extra space.

Related interview topics

Arrays & Strings Coding Questions FAQ

Two pointers (left/right or fast/slow), sliding window (fixed or variable), prefix sums (range queries), and hash map for O(1) lookup. Two pointers + sliding window cover roughly 40% of array interview problems.

Sort the array first — duplicates become adjacent and easy to skip. After fixing a pointer at index i, skip it forward while nums[i] === nums[i-1]. This pattern appears in 2Sum, 3Sum, 4Sum, and combination problems.

Use prefix sums when you need to answer multiple range sum queries efficiently. Precompute prefix[i] = sum of nums[0..i−1]. Range sum(l, r) = prefix[r+1] − prefix[l]. O(n) build, O(1) per query. Key for subarray sum problems.

If you need exact O(n) time and O(n) space is acceptable, use a hash map — it's faster and avoids sorting overhead. If you need O(1) extra space or the output must be sorted, sort first then use two pointers. For counting frequencies, the hash map (or character array) is almost always right.

Practice Arrays & Strings Coding Questions with real-time AI.

Stop reading sample answers and start practicing. Get personalized feedback on structure, delivery, and depth — every time.

Free plan available · No credit card needed · Cancel anytime