Medium · Two pointers · spoken drill
Free AI LeetCode Communication Coach — 3Sum
Sort, fix i, then two pointers for the pair that sums to −nums[i]. Deduplicate out loud.
The problem
Given an integer array nums, return all unique triplets that sum to zero.
Example: nums = [-1,0,1,2,-1,-4] → [[-1,-1,2],[-1,0,1]]
Sort + two pointers + duplicate skip.
nums.sort()
res = []
for i in range(len(nums)):
if i and nums[i] == nums[i-1]:
continue
left, right = i + 1, len(nums) - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total == 0:
res.append([nums[i], nums[left], nums[right]])
left += 1
while left < right and nums[left] == nums[left-1]:
left += 1
elif total < 0:
left += 1
else:
right -= 1
return resHow to explain it
1. Restate
Say the problem in your own words.
One or two sentences. Show you understood the input, the output, and the goal — not that you memorised the prompt.
2. Approach
Name the method before you code.
Brute force first if you need it, then the structure you will use: hash map, two pointers, stack, binary search.
3. Example
Walk one concrete input.
Pick small numbers. Say what you store, what you compare, and what you return. Interviewers follow an example more easily than abstract talk.
4. Time and space
One sentence each.
After the example, before you claim you are done. “Time is O(n) because we scan once. Space is O(n) for the map.”
5. Edge cases
Name at least one unusual input.
Empty input, duplicates, already sorted, overflow. Invite a follow-up: “I would also check …”
- “I sort, fix one index, then two pointers.”
- “I skip duplicates on i, left, and right.”
Practise out loud
3 free scored runs left this hour.
Problem · Medium
Given an integer array nums, return all unique triplets that sum to zero.
Example: nums = [-1,0,1,2,-1,-4] → [[-1,-1,2],[-1,0,1]]
- 1Restate — Say the problem in your own words.
- 2Approach — Name the method before you code.
- 3Example — Walk one concrete input.
- 4Time and space — One sentence each.
- 5Edge cases — Name at least one unusual input.
Hit record. Short countdown, then 60–90 seconds. We score the five-step script — never pronunciation.
Recording needs Chrome or Edge. You can still type below.
Model spoken script (~75s)
1. Restate
I need every unique triplet that adds to zero.
2. Approach
I sort the array. For each index i I run two pointers on the rest looking for a pair that sums to minus nums[i]. I skip duplicates.
3. Example
On −1,0,1,2,−1,−4 after sort, when i is −1 I find −1,2 and 0,1. I skip the next −1 so I do not repeat.
4. Time and space
Time is O(n²). Extra space is O(1) besides the output.
5. Edge cases
I would check arrays shorter than three and the all-zeros case.
How to explain 3Sum out loud, step by step
In one sentence: You have to list every distinct group of three numbers from the array whose total is exactly zero, without repeating the same group twice.
What to say, in order
- “The output is a list of unique triplets with a sum of zero, so duplicate handling is half of this problem.”
- “First I sort the array. Sorting lets me use two pointers and makes duplicates sit next to each other.”
- “Then I fix one number at index i. For the rest of the array I need a pair that sums to minus that number.”
- “I put a left pointer just after i and a right pointer at the end. If the sum is too small, I move left forward; if it's too big, I move right back.”
- “When I find a zero sum, I record the triplet, then move both pointers and skip over equal values so I don't record the same triplet again.”
- “I also skip i when it has the same value as the previous i. And once the fixed number is positive, I can stop, because three positives can't sum to zero.”
Key English phrases for 3Sum
- fix one element
- — choose the first number of the triplet in the outer loop
- reduce it to Two Sum
- — the inner problem is a pair sum on a sorted range
- skip duplicates
- — advance past equal neighbours after sorting
- too small / too big
- — the rule for which pointer to move
- early exit
- — stopping when the fixed number becomes positive
How to say the time and space complexity
“Time is O(n squared): sorting is O(n log n), and for each of the n fixed numbers the two-pointer scan is O(n). Space is O(1) extra apart from the output, though the sort itself may use some extra memory depending on the language.”
Say 'n squared dominates n log n' so the interviewer hears why the sort doesn't change the final bound.
Communication mistakes on 3Sum
- Mistake: Saying 'I'll use a set to remove duplicate triplets' as the main plan. Instead: Say 'Because the array is sorted, I skip equal neighbours, so duplicates never get added.'
- Mistake: Saying 'three pointers' without explaining which one is fixed. Instead: Say 'one fixed index in the outer loop, and two moving pointers inside it.'
- Mistake: Giving O(n log n) as the final time because you mentioned sorting last. Instead: Finish with the dominant term: 'Overall it's O(n squared).'
- Mistake: Mumbling the pointer rule. Instead: Use one clear sentence: 'If the sum is below zero I move left; above zero I move right.'
Likely follow-up questions
Interviewer: “How would you find the triplet whose sum is closest to a target?”
You: “Same skeleton: sort, fix i, two pointers. Instead of checking for zero, I track the sum with the smallest absolute difference from the target and move the pointers the same way. Still O(n squared).”
Interviewer: “How does this extend to four numbers?”
You: “I add one more outer loop: fix two numbers, then run two pointers on the rest. That gives O(n cubed), with the same duplicate skipping at every level.”
Common mistakes when explaining 3Sum
- Forgetting to say how you skip duplicates.
- Claiming O(n) when the two-pointer loop is O(n²).
Other problems
Easy · Hash map
Two Sum
Classic hash-map scan. Name brute force, then the map, walk [2,7,11,15] target 9, finish with O(n).
Medium · Hash map
Group Anagrams
Group strings by anagram signature — sorted key or 26-letter count — then walk one bucket.
Easy · Two pointers
Valid Palindrome
Two pointers from both ends; skip junk characters; compare lowercase letters.
Medium · Sliding window
Longest Substring Without Repeating Characters
Grow a window; when a duplicate appears, move the left edge past its last index.
Easy · Stack
Valid Parentheses
Stack story: push openers, pop on matching closer, empty stack at the end.
Medium · Stack
Daily Temperatures
Monotonic stack of indices waiting for a warmer day; answer[i] is days to wait.
FAQ
Questions
More questions? Email us at contact@mocklyenglish.com.
Course: How to explain a LeetCode solution · Think out loud · Explain code out loud