Skip to main content

1. Two Sum

easyAsked at HubSpot

HubSpot uses Two Sum as a warm-up screen to see if you can jump straight from brute force to an O(n) hash-map solution while explaining your reasoning out loud — a habit their Boston engineering culture values deeply.

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

Source citations

Public interview reports confirming this problem appears in HubSpot loops.

  • Glassdoor (2026-Q1)Recurring report of Two Sum appearing in HubSpot phone-screen rounds as an algorithmic warm-up.
  • Blind (2025-11)Multiple HubSpot SWE threads list Two Sum as a common first question to gauge hash-map fluency.

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)

For each element, scan every element after it to find the complement. Simple but O(n²) — mention it only as a baseline before pivoting to the hash-map approach.

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: No extra space, but quadratic time. Acceptable to state but never ship as a final answer at HubSpot — they expect you to spot the hash-map optimization immediately.

2. One-pass hash map

Store each number's index in a map as you iterate. For each element, check if its complement (target - current) is already in the map. If yes, return both indices immediately.

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

Tradeoff: Single pass, O(n) time and space. This is the expected production-quality answer. HubSpot interviewers will push you to explain why you store the complement check before inserting the current element — to avoid using the same index twice.

HubSpot-specific tips

HubSpot interviewers want you to verbalize the progression: 'Brute force is O(n²). I can trade space for time using a hash map and get O(n).' Then code the hash-map version directly. They appreciate candidates who acknowledge edge cases (duplicate values, negative numbers) without being prompted. Think of it like CRM data deduplication — hash maps are the bread-and-butter lookup structure.

Common mistakes

  • Using the same index twice — for example, returning [0, 0] when nums = [4, ...] and target = 8.
  • Storing the complement instead of the current value — insert nums[i] → i, not complement → i.
  • Forgetting that the problem guarantees exactly one solution, so no tie-breaking logic is needed.
  • Returning values instead of indices — re-read the problem statement carefully.

Follow-up questions

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

  • Two Sum II — what if the array is sorted? (Two-pointer in O(1) space.)
  • Three Sum — find all unique triplets that sum to zero.
  • What if there could be zero or multiple valid pairs — how would you collect all of them?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why insert after checking the complement?

Inserting before checking would let you match an element against itself. By checking first, you ensure the complement was seen at a different index.

Does this handle duplicates in the array?

Yes — if nums = [3, 3] and target = 6, the first 3 is stored with index 0; when you reach the second 3, its complement 3 is already in the map at index 0, so you correctly return [0, 1].

What data structure powers the O(1) lookup?

A hash map (JavaScript Map or object). Average O(1) get and set; worst-case O(n) with hash collisions, but that's rare and irrelevant for interview purposes.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →