Aivyns
AS

Coding Assessment

Solve real-world problems in a sandboxed editor with AI feedback.

45:00 remaining
AI Proctor on
Problem set

Two Sum

Arrays · Hash Map

Given an array of integers `nums` and an integer `target`, return the indices of the two numbers that add up to `target`. You may assume exactly one solution exists, and you may not use the same element twice.

Examples
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
nums[0] + nums[1] == 9
Input: nums = [3,2,4], target = 6
Output: [1,2]
Constraints
  • 2 ≤ nums.length ≤ 10^4
  • -10^9 ≤ nums[i] ≤ 10^9
  • Only one valid answer exists

AI Hint

AI

Try using a hash map to store value → index. This reduces lookup to O(1) and overall complexity to O(n).

solution.js
1
2
3
4
5
6
7
8
9
Case #1
in: [2,7,11,15], 9
expected: [0,1]
Case #2
in: [3,2,4], 6
expected: [1,2]
+ hidden test cases will run on Submit