Two Sum Mock Interview

  1. Problem
  2. 2Clarifying Questions
  3. 3Constraints
  4. 4Brute Force
  5. 5Complexity Analysis
  6. 6Pattern Recognition
  7. 7Optimized Solution
  8. 8Implementation
  9. 9Testing
  10. 10Follow-Up
  11. 11Evaluation
Problem

Given an array of integers nums and an integer target, return the indices of the two distinct elements whose values add up to target. You may assume exactly one valid answer exists, and you may not use the same element twice. The indices may be returned in any order.

Constraints
  • 2 ≤ n ≤ 10^5
  • -10^9 ≤ nums[i] ≤ 10^9
  • -10^9 ≤ target ≤ 10^9
  • exactly one valid pair exists
Example
in: nums = [2,7,11,15], target = 9
out: [0,1]

Clarify

Before choosing anything: what would you ask the interviewer? What assumptions are you making? (Duplicates? Empty input? Value ranges? What to return when there is no answer?)