Easy👤 0-2 years👤 3-5 years 1 min read

Two Sum Interview Question

Asked inAmazonAccentureDeloitte
Report issue

Coffee Chat Question

Concept Made Simple

Solve Two Sum — return indices of two numbers adding to a target.

🧠Mind Map Answer

Remember It Faster

Brute force is O(n²) with nested loops. The trick: as you scan, remember what you have seen in a HashMap so you can look up the complement in O(1).

Needtarget - current
Storevalue → index seen so far
TimeO(n), one pass

⌨️Hands-on Keyboard

Learn by Doing

python
def two_sum(nums, target):
    seen = {}
    for i, n in enumerate(nums):
        if target - n in seen:
            return [seen[target - n], i]
        seen[n] = i

print(two_sum([2, 7, 11, 15], 9))
Output
[0, 1]

🔥What If?

Think Beyond the Expected

What if the array is already sorted?

Use the two-pointer technique — one at each end, move them inward based on the sum. That is O(n) time and O(1) space, beating the HashMap on memory.

😂Real World

Two Sum is the 'hello world' of the HashMap-for-lookup pattern. The same trick — remember what you've seen so the complement is O(1) — shows up in deduplication, caching, and detecting pairs/anagrams across real codebases.

🎯Interviewer's Expectation

Keywords they're listening for:

brute force O(n²) firstHashMap → O(n)space-time trade-offtwo-pointer if sorted

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: HashMaps (Coding Challenges)
Interview question: "Solve Two Sum — return indices of two numbers adding to a target."

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions