Back to problems

Two Sum with Smallest-Indices Tiebreak (+ 3 Sum Follow-up)

Algorithm · Walmart Labs · Medium

You are given an integer array nums and an integer target. Implement two_sum(nums, target), which returns two indices (i, j) with i < j satisfying $$nums[i] + nums[j] = target$$. When more than one such pair exists, pick the pair whose first index i is as small as possible; if two pairs tie on i, pick the one with the smaller j. If no pair adds up to target, return a sentinel such as [-1, -1] or an empty result. After the two-sum version is complete, the interviewer will…

Checking your access…