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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
Master two pointers and sliding window — they solve the majority of array/string problems in O(n).
Hash maps convert O(n²) brute force to O(n) for lookup-heavy problems — always consider them first.
Sort first if the problem involves finding pairs or groups — sorting enables two-pointer and deduplication.
Practice in-place manipulation (reversals, rotations) — they test pointer/index control without extra space.
Related interview topics
Arrays & Strings Coding Questions FAQ
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