Two Sum Mock Interview - Game Show Variant
Two Sum Game Show Variant Mock Interview
In this mock technical interview, a candidate is asked to solve a classic array problem involving selecting two items whose sum matches a gift card amount. Watch how the developer clarifies ambiguities, performs a dry run, and walks through both brute-force and optimized approaches — just like in a real FAANG interview setting.
Sample Array
Gift Card Amount
Valid Pair
Return Indices
- If prices = [10, 20, 30, 40, 80, 100] and gift card = 60 → [1, 3] is valid because 20 + 40 = 60.
- If prices = [25, 25, 50] and gift card = 50 → valid indices would be [0, 1] and not [0, 0].
- If prices = [1, 2, 3, 4, 5] and gift card = 9 → answer is [3, 4] since 4 + 5 = 9.
- Check 10 + 20 = 30 not equal to target 60 ❌
- 10 + 30 = 40 not equal to target 60 ❌
- 10 + 40 = 50 not equal to target 60 ❌
- 10 + 80 = 90 not equal to target 60 ❌
- 10 + 100 = 110 not equal to target 60 ❌
- Check 20 + 30 = 50 ❌ not equal to target 60
- 20 + 40 = 60 equal to target → indices [1, 3]
I can use two nested loops: the outer loop will pick the first item, and the inner loop will check every other item after it to see if the sum of the two equals the target amount. If a match is found, I’ll return their indices immediately. Since the problem says there's atleast one solution, I don’t need to worry about handling multiple results or no solution.
for (let i = 0; i < prices.length; i++) {
for (let j = i + 1; j < prices.length; j++) {
if (prices[i] + prices[j] === giftCardAmount) {
return [i, j];
}
}
}
O(n²)
, where n
is the number of items.If it is, that means I’ve found two prices that add up to the gift card amount, so I can return their indices immediately.
const seen = new Map();
for (let i = 0; i < prices.length; i++) {
const complement = giftCardAmount - prices[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
}
seen.set(prices[i], i);
}
- On first 25 (index 0), I store it in the map.
- Second 25 (index 1), complement is 25 → map has it → return [0, 1].
- Random order inputs
- Duplicates
- Negative values (if allowed — though not mentioned in this case)
Great walkthrough. You approached the problem methodically, starting with clarifications and dry-running through the example, which showed good attention to detail. Your transition from brute-force to an optimized hash map solution was well-articulated and efficient.
One strength was your ability to anticipate edge cases, like duplicate values and index handling. Your explanation of time and space complexity was also clear and concise.
In a real interview setting, I’d encourage you to verbalize trade-offs a bit more, especially around why one solution might be preferred based on input size or constraints. But overall — very strong performance!
Gist of this Mock Interview
The flowchart below shows the key steps and decisions made during the mock interview, from understanding the problem to arriving at an optimized solution. It helps visualize how the conversation and thought process unfolded.
Comments
Loading comments...