2dbi
Home/Apple/Two Sum
AApple·DSASDE-1Online Assessment

Two Sum

Problem

Given an array nums and a target, return indices of the two numbers that add up to the target. Exactly one solution exists; you may not use the same element twice.

Example

nums = [2,7,11,15], target = 9
Output: [0, 1]

Constraints

  • 2 ≤ nums.length ≤ 10^4
  • Only one valid answer exists

Expected

O(n) using a hash map. Apple interviewers care about clean code and edge cases (negatives, duplicates) more than speed of arrival at the answer.

added 6 days ago
LeadersAccount