← Walmart Interview Insights

Walmart·Software Engineer·Online Assessment (OA)·Junior

JuniorRejected
May 2026Remote

Summary

Took an OA for a software engineering role and got a tricky string manipulation problem. Thought my solution was solid enough since it passed the sample cases, but got rejected anyway. Still not sure exactly where my logic broke down.

Questions Asked (1)

Q1

Given a string of decimal digits representing a non-negative integer, delete any subset of digits (preserving order) to produce the largest possible number where every digit that appears does so an even number of times.

Algorithms & Data Structures
Author's notes

My approach was a monotonic stack where I'd pop the top if it was smaller than the incoming digit and had an odd frequency, figuring that'd maximize the result.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a greedy strategy: process digits from left to right, maintaining a stack of selected digits. For each digit, decide whether to include it based on whether it can improve the final number while ensuring all counts are even, and handle the last occurrence of each digit carefully to avoid odd counts.

Pro tip: Clarify that the result should have no leading zeros unless the number is zero, and discuss how to handle that constraint within the greedy approach.

1. Understand the problem and constraints

Restate the problem: delete a subset of digits to form the largest number where each digit appears an even number of times. Note that the order of remaining digits is preserved, and the result can be empty (which represents 0).

2. Identify the greedy choice

To maximize the number, we want the leftmost digits as large as possible. For each digit, we can decide to include it if it helps to form a larger number, but we must ensure that all digits end up with even counts.

3. Design the algorithm

Use a stack to build the result. Iterate through the digits, and for each digit, while the stack is not empty and the top of the stack is less than the current digit and the top digit can be removed (i.e., it appears again later to maintain even count), pop it. Then push the current digit if it can be part of an even count (e.g., if it's not the last occurrence or if we can pair it).

4. Handle edge cases and finalize

After processing all digits, ensure all counts are even by possibly removing the last occurrence of any digit with odd count. Remove leading zeros if the result is not empty. If the result is empty, return '0'.

5. Analyze complexity and test

The algorithm runs in O(n) time and O(n) space. Test with examples like '1234' (result '0'), '4444' (result '4444'), '123321' (result '123321'), and '1111' (result '1111').

Key Points to Mention

  • Greedy approach with a stack to build the largest number
  • Tracking the last occurrence of each digit to decide if a digit can be removed
  • Ensuring even counts by possibly removing one occurrence of digits with odd counts at the end
  • Handling leading zeros by removing them unless the result is zero
  • Time and space complexity: O(n) time and O(n) space
  • Edge cases: empty result, all digits same, no valid even-count subset

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