1. Two Sum
easyAsked at CohereGiven an integer array and a target, return indices of the two numbers that add up to the target. Cohere asks this as a warm-up because hash-map lookup is the foundation of embedding deduplication and nearest-neighbor retrieval at scale.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Cohere loops.
- Glassdoor (2026-Q1)— Reported in Cohere SWE phone-screen threads as a standard warm-up problem.
- Blind (2025-11)— Multiple Cohere candidates confirm Two Sum appears in early-round screening.
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^9Only one valid answer exists.
Examples
Example 1
nums = [2,7,11,15], target = 9[0,1]Explanation: nums[0] + nums[1] = 2 + 7 = 9.
Example 2
nums = [3,2,4], target = 6[1,2]Explanation: nums[1] + nums[2] = 2 + 4 = 6.
Approaches
1. Brute force
Check every pair of indices. Simple but O(n²) — only acceptable for tiny inputs.
- 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 makes it impractical for large embedding lookup tables.
2. Hash map (one pass)
Store each number's index in a map as you iterate. For each element, check whether the complement (target - nums[i]) is already in the map.
- Time
- O(n)
- Space
- O(n)
function twoSum(nums, target) {
const seen = new Map();
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: O(n) time and space — the canonical answer. Mirrors how an embedding lookup table avoids redundant distance computations by caching partial results.
Cohere-specific tips
Cohere interviewers often extend Two Sum by asking how you would handle a streaming data source — e.g., embeddings arriving in batches from an inference API. Mention that the hash-map approach extends naturally to streaming because you only need to maintain one pass over the data. If they ask about large-scale retrieval, connect it to inverted indexes and approximate nearest-neighbor search.
Common mistakes
- Using the same element twice — the complement check must use a separate index (i != j).
- Returning values instead of indices — read the problem statement carefully.
- Not handling negative numbers — the hash-map approach works regardless of sign.
- Two-pass approach when one pass suffices — mention the optimization proactively.
Follow-up questions
An interviewer at Cohere may pivot to one of these next:
- Two Sum II — input is sorted; use two pointers for O(1) space.
- Three Sum — fix one element and reduce to Two Sum on the rest.
- How would you adapt this for a streaming data source where nums arrives in chunks?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why a Map instead of an object?
Map handles integer keys without prototype pollution and guarantees O(1) average lookup. For numeric keys both work, but Map is idiomatic.
Can the same index appear in both positions?
No — the problem states you may not use the same element twice, so store the index only after checking the complement.
Does order of the returned pair matter?
No — the problem says you can return in any order.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Cohere interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →