Compute the Price of a Nested Bundle Expression

Last updated: November 29, 2026

Quick Overview

Bundles are written as expressions like `2(milk 3egg) bread`, where a number multiplies the SKU or parenthesized group that follows it. Given a price table in cents, return the total price, rejecting malformed input.

Walmart Labs
Coding & Algorithms
Software Engineer
Walmart Labs
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Bundles are written as expressions like `2(milk 3egg) bread`, where a number multiplies the SKU or parenthesized group that follows it. Given a price table in cents, return the total price, rejecting malformed input.

Framed around promotion and bundle definitions in Walmart's pricing systems, which are often authored as compact strings. The interviewer checks a clean tokenizer, a stack for nesting, and real validation of unbalanced parentheses and unknown SKUs.

What the Interviewer Expects
  • Define the grammar before coding
  • Tokenize numbers, names and parentheses
  • Use a stack of partial totals with group multipliers
  • Validate unbalanced parentheses, dangling counts and unknown SKUs
  • Use integer cents rather than floating point
Key Topics to Cover
string parsing
stacks
tokenization
input validation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add a percent discount operator such as `(milk egg)*90%`?
  • How would you return an itemized breakdown instead of a total?
  • How would you write this as a recursive descent parser, and what are the trade-offs?
  • How would you protect against a deeply nested expression from an untrusted source?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Grammar: expr := term* and term := [count] (sku | '(' expr ')'). SKUs are lowercase words, counts are non-negative integers, whitespace is ignored. Output: total in integer cents. Errors: unknown ...

High-Level Design

A regex tokenizer reads one token at a time from the current position. A stack holds frames [total, multiplier]. A count is held as pending until the next term. A SKU adds count * price to the t...


Submit Your Answer
Markdown supported