1. Two Sum
easyAsked at SalesforceFind two indices in an array whose values sum to a target. Salesforce uses this as a warmup to test whether you reach for a hash-map lookup immediately or default to a nested loop on every problem.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce MTS phone-screen reports cite Two Sum as the standard warmup.
- LeetCode Discuss (2025)— Recurring at Salesforce intern and new-grad screens.
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] == 9
Example 2
nums = [3,2,4], target = 6[1,2]Approaches
1. Brute force nested loop
Try every pair (i, j) and check if they sum to target.
- Time
- O(n^2)
- 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];
}
}
}Tradeoff: Works on small inputs but Salesforce interviewers will push you to do better — they want to see you trade memory for time.
2. Hash map single-pass
Walk the array once; for each value v, check if target - v is already in the map. If yes, you have your pair. Otherwise, store v's index.
- Time
- O(n)
- Space
- O(n)
function twoSum(nums, target) {
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const need = target - nums[i];
if (seen.has(need)) return [seen.get(need), i];
seen.set(nums[i], i);
}
}Tradeoff: O(n) time at the cost of O(n) space — the classic trade Salesforce wants you to articulate.
Salesforce-specific tips
Salesforce interviewers grade this on time-to-optimal — they expect you to articulate the hash-map trade-off within 60 seconds. Bonus signal: mention that this is a special case of k-Sum and reaches O(n) by collapsing the inner loop into a map lookup. Salesforce also values clean variable names (seen, need) over single-letter ones in their internal style guide.
Common mistakes
- Returning values instead of indices — the prompt specifically asks for indices.
- Storing the index BEFORE checking for the complement — causes off-by-one when the array contains a duplicate that sums to target.
- Using an Object instead of a Map — fine for ASCII keys but breaks if numbers are negative and you coerce to string keys badly.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- What if the array is sorted? (Two-pointer, O(n) time, O(1) space.)
- What if you need all pairs that sum to target, not just one?
- Generalize to 3Sum or k-Sum — what changes?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the hash map approach O(n) space and not O(1)?
In the worst case (no pair found until the last element), you store every prior index in the map. Salesforce will dock you if you claim O(1) space here.
Can I sort and use two pointers instead?
Yes, but sorting loses the original indices, so you'd need to track them separately, which usually ends up no cleaner than the hash map. Mention it as an alternative but default to the hash map.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →