PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates array manipulation, index-level reasoning, and algorithmic efficiency for locating pair sums under lexicographic tie-breaking, testing attention to ordering constraints and correct index selection.

  • medium
  • Walmart Labs
  • Coding & Algorithms
  • Software Engineer

Implement lexicographically smallest Two Sum

Company: Walmart Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an unsorted integer array `nums` and an integer `target`. Return the indices `[i, j]` (0-based) such that `i < j` and `nums[i] + nums[j] == target`. If there are multiple valid pairs, return the pair with the **smallest `i`**. If there is still a tie (same `i`), return the one with the **smallest `j`**. Assume at least one valid pair exists. Follow-up (discussion only): Explain an efficient approach to find all triplets `(i, j, k)` with `i < j < k` such that `nums[i] + nums[j] + nums[k] == 0`, and you may ignore duplicate triplets (or assume all numbers are distinct).

Quick Answer: This question evaluates array manipulation, index-level reasoning, and algorithmic efficiency for locating pair sums under lexicographic tie-breaking, testing attention to ordering constraints and correct index selection.

You are given an unsorted integer array `nums` and an integer `target`. Return the indices `[i, j]` (0-based) such that `i < j` and `nums[i] + nums[j] == target`. If there are multiple valid pairs, return the pair with the smallest `i`. If there is still a tie (same `i`), return the one with the smallest `j`. In other words, return the lexicographically smallest valid index pair. You may assume that at least one valid pair exists. Follow-up (discussion only, not part of the required implementation): Explain an efficient approach to find all triplets `(i, j, k)` with `i < j < k` such that `nums[i] + nums[j] + nums[k] == 0`, ignoring duplicate triplets (or assuming all numbers are distinct).

Constraints

  • 2 <= len(nums) <= 2 * 10^5
  • -10^9 <= nums[i], target <= 10^9
  • `nums` is unsorted
  • At least one valid pair exists

Examples

Input: ([2, 7, 11, 15], 9)

Expected Output: [0, 1]

Explanation: `nums[0] + nums[1] = 2 + 7 = 9`, and this is the only valid pair.

Input: ([1, 4, 5, 3, 2], 6)

Expected Output: [0, 2]

Explanation: Valid pairs are `[0, 2]` (1 + 5) and `[1, 4]` (4 + 2). The pair `[0, 2]` is lexicographically smaller because it has the smaller first index.

Input: ([3, 3], 6)

Expected Output: [0, 1]

Explanation: This is the smallest possible array length with a valid answer. The only pair is `[0, 1]`.

Input: ([0, -1, 2, -3, 1], -2)

Expected Output: [3, 4]

Explanation: `nums[3] + nums[4] = -3 + 1 = -2`. No earlier valid pair exists.

Input: ([5, 1, 5, 1], 6)

Expected Output: [0, 1]

Explanation: There are multiple valid pairs: `[0, 1]`, `[0, 3]`, `[1, 2]`, and `[2, 3]`. The lexicographically smallest is `[0, 1]`.

Input: ([4, 6, 1, 3, 5, 2], 7)

Expected Output: [0, 3]

Explanation: Valid pairs include `[1, 2]` (6 + 1), `[0, 3]` (4 + 3), and `[4, 5]` (5 + 2). Even though `[1, 2]` appears earlier during a left-to-right scan, `[0, 3]` is the correct answer because it has the smaller first index.

Hints

  1. Use a hash map from value to its first occurrence index so you can check whether the needed complement has appeared before in O(1) time.
  2. Do not stop at the first valid pair you find. Keep comparing candidates using lexicographic order: smaller `i` first, then smaller `j`.
Last updated: May 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Check whether brackets are balanced - Walmart Labs (medium)
  • Compute days until plants stop dying - Walmart Labs (medium)
  • Count ways to make change (DP) - Walmart Labs (medium)
  • Find shared courses between student pairs - Walmart Labs (medium)
  • Merge two sorted arrays in-place - Walmart Labs (easy)