Skip to main content

1. Two Sum

easyAsked at HP

HP's firmware and driver teams frequently use look-up tables for O(1) dispatch — Two Sum tests whether you instinctively reach for a hash map instead of a brute-force scan, which matters when your code runs on embedded microcontrollers with tight loop budgets.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in HP loops.

  • Glassdoor (2026-Q1)Reported in HP Software Engineer phone-screen round feedback as a warm-up problem.
  • Blind (2025-11)HP interview prep threads list Two Sum among the most common first-round questions for SWE roles.

Problem

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Constraints

  • 2 <= nums.length <= 10^4
  • −10^9 <= nums[i] <= 10^9
  • −10^9 <= target <= 10^9
  • Only one valid answer exists.

Examples

Example 1

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

Explanation: nums[0] + nums[1] = 2 + 7 = 9, so return [0, 1].

Example 2

Input
nums = [3,2,4], target = 6
Output
[1,2]

Explanation: nums[1] + nums[2] = 2 + 4 = 6.

Approaches

1. Brute force (nested loops)

Try every pair of elements and check if they sum to target.

Time
O(n²)
Space
O(1)
function twoSum(nums, target) {
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) return [i, j];
    }
  }
  return [];
}

Tradeoff: Simple to explain but O(n²) time is unacceptable for large arrays. State this approach first to show you understand the problem, then optimize.

2. Hash map (one pass)

Store each number's index in a map. For each element, check if its complement (target − num) already exists in the map.

Time
O(n)
Space
O(n)
function twoSum(nums, target) {
  const map = new Map(); // value → index
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (map.has(complement)) {
      return [map.get(complement), i];
    }
    map.set(nums[i], i);
  }
  return [];
}

Tradeoff: O(n) time and O(n) space. The canonical answer HP expects. The one-pass approach is preferred over a two-pass approach because you complete the lookup in a single scan — important for pipelines that process large data streams.

HP-specific tips

HP interviewers appreciate clean variable naming and explicit reasoning about trade-offs — say out loud why O(n²) is unacceptable before jumping to the hash-map solution. In the context of HP's embedded and enterprise software, mention that hash maps trade memory for speed, which is a considered choice in constrained environments. Expect a follow-up about handling duplicates in the input.

Common mistakes

  • Using the same element twice — check that the complement index differs from the current index.
  • Forgetting to store the current element in the map when no complement is found yet.
  • Returning indices in the wrong order — the problem says any order is fine, but confirm with the interviewer.
  • Overcomplicating with sorting — sorting destroys original indices, requiring extra bookkeeping.

Follow-up questions

An interviewer at HP may pivot to one of these next:

  • What if the array is sorted — can you do it in O(1) extra space using two pointers?
  • What if there could be multiple valid pairs — return all of them.
  • How would you handle this on a memory-constrained embedded device where a hash map is too expensive?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Can I sort the array first?

Sorting lets you use two pointers in O(n log n) time and O(1) space, but it destroys the original indices. You'd need to store (value, index) pairs before sorting, adding complexity. The hash-map approach is simpler and faster.

Why one pass instead of two?

A two-pass approach first loads the map then queries it, requiring two loops. One-pass interleaves both steps, halving the constant and terminating early once the pair is found.

What if nums has duplicate values?

The map stores each value's most recent index. If the complement equals the current value, the map already holds the earlier index, so no collision occurs.

Practice these live with InterviewChamp.AI

Drill Two Sum and other HP interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →