← Walmart Labs Interview Insights

Walmart Labs·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Interviewed for a software engineer role at Walmart Labs. One coding question, fairly focused on binary search with a twist around duplicate handling.

Questions Asked (1)

Q1

Given a sorted array containing duplicates and a target number, return the index of the first occurrence of that target. For example, with target 8 in [1, 3, 5, 8, 8, 8, 8, 8, 9, 10, 15], you should return the index of the leftmost 8.

Algorithms & Data Structures
Author's notes

I knew binary search immediately but the duplicate part is where I fumbled a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a modified binary search to find the leftmost occurrence of the target. When the target is found, continue searching in the left half to check for earlier occurrences. This ensures O(log n) time complexity even with duplicates.

Pro tip: Emphasize that this approach is optimal for large datasets and mention that it can be easily adapted to find the last occurrence or count occurrences. Also, clarify how you handle edge cases like empty arrays or target not present.

1. Clarify the problem and constraints

Confirm that the array is sorted, may contain duplicates, and that you need the first occurrence. Ask about edge cases: empty array, target not present, all elements are the target.

2. Choose the algorithm

Select binary search over linear scan for efficiency. Explain that binary search can be modified to find the leftmost occurrence by continuing to search left even after finding the target.

3. Implement the modified binary search

Initialize low and high pointers. While low <= high, compute mid. If array[mid] < target, move low to mid+1; if array[mid] > target, move high to mid-1; if equal, record mid as a potential answer and move high to mid-1 to search left.

4. Handle edge cases and return result

After the loop, return the recorded index if found, else return -1. Discuss how the algorithm handles duplicates and ensures the first occurrence is found.

5. Analyze complexity and test

State time complexity O(log n) and space O(1). Walk through the example to verify correctness, and consider additional test cases like target at the beginning or end.

Key Points to Mention

  • Binary search is optimal for sorted arrays with O(log n) time complexity.
  • Modify binary search to continue searching left when target is found to get the first occurrence.
  • Handle edge cases: empty array, target not present, all elements equal to target.
  • Use low <= high loop condition and update pointers correctly to avoid infinite loops.
  • Return -1 if target is not found.
  • Mention that the same approach can be adapted to find the last occurrence or count occurrences.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.