Asked at 12 companies
12 recent
- Accenture
- Amazon
- Bloomberg
- Capgemini
- HCL
- Infosys
- Microsoft
- tcs
- Wipro
+2 moreShow fewer
- Cognizant
- Meta
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Counting subarrays with a map of totals
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Caches and duplicate detection: A hash map answers “have I seen this before?” in one step. It's how apps cache results, and how a photo app spots the same picture uploaded twice.
ProblemProblem statementGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.ReadHide
💡Problem Statement
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use hash map to store complements for O(n) lookup
🔑 Key Pattern & Recognition
This problem is the canonical example of the "Hash Map Complement" pattern.
| Pattern Name | Goal | How to Recognize |
|---|---|---|
| Hash Map Complement | Find two elements in an array/list that satisfy a specific condition (e.g., sum up to a target, difference equals K). | The input is an unsorted array, and you need to find a pair whose relationship (sum, difference, etc.) equals a target value. |
| The Trick | Convert the two-sum problem into a one-sum check. For every element x, instantly check if the required complement (target - x) has already been seen. |
This pattern allows you to solve two-pointer or two-value problems in O(n) time by sacrificing O(n) space for the hash map, which stores previously encountered values for O(1) lookup.
🛠️ Brute Force Approach (O(n²))
Approach: Use nested loops. The outer loop selects the first number, and the inner loop checks every other number against it to find the pair that sums to the target.
Time Complexity: O(n²)
Space Complexity: O(1)
⚡ Optimized Approach (O(n))
Approach: We use the Hash Map Complement pattern. We iterate through the array only once, using a Hash Map (Dictionary) to track values we've already processed and their indices.
- Iterate through the array using index
iand valuex. - Calculate the complement needed:
C = target - x. - Check if
Cis already a key in the Hash Map. If yes, we found our pair: returnmap[C]andi. - If not found, add the current number and its index to the map:
map[x] = i.
Time Complexity: O(n) (Single pass with O(1) average lookup/insertion) Space Complexity: O(n) (To store the Hash Map)
Solution code
from typing import List
class Solution:
# 1. Optimized Approach: Time O(n), Space O(n) - Dictionary/Hash Map
def twoSum_optimized(self, nums: List[int], target: int) -> List[int]:
num_map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_map:
return [num_map[complement], i]
num_map[num] = i
return [] # Should not be reached
# 2. Brute Force Approach: Time O(n^2), Space O(1)
def twoSum_brute(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] # Should not be reached
# Default entry point (use the optimized one for general use)
def twoSum(self, nums: List[int], target: int) -> List[int]:
return self.twoSum_optimized(nums, target)Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language