1. Two Sum

1. Two Sum

Easy
50.0%
1.3k
arrayhash-tabletwo-pointers
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Twitter (X), Oracle

You are interviewing for a backend software engineer role at a FAANG-level technology company.
The interviewer presents a real-world system scenario instead of a textbook question.

A large-scale platform processes numerical data such as:

  • Transaction amounts
  • Inventory weights
  • Ad bidding values
  • System performance metrics

For analytics and optimization, the system frequently needs to identify two distinct values whose sum equals a specific target value.

Your Task

Given:

  • An integer array nums
  • An integer target

Return the indices of the two distinct elements in nums whose sum equals target.

Important Rules

  • Each element can be used only once
  • Exactly one valid solution exists
  • Return the indices, not the values
  • Indices may be returned in any order
  • The solution must be efficient and scalable

This is a common practice coding problem for DSA question in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Examples

Example 1:

Input: nums = [2, 7, 11, 15] target = 9
Output: [0, 1]
Explanation: The values at indices 0 and 1 sum to 9, meeting the system’s target threshold.

Constraints

  • 2 <= nums.length <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Exactly one valid solution exists
  • Output indices can be returned in any order

Loading editor...

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

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback