The "Apply Discount to Prices interview question" is a string parsing and formatting challenge. You are given a sentence containing words and prices (represented as a '$' followed by a number). Your task is to find all valid prices and reduce them by a given percentage. The final sentence must be returned with the discounted prices formatted to exactly two decimal places.
Amazon uses the "Apply Discount to Prices coding problem" to test a candidate's string manipulation skills and attention to detail. It requires identifying what constitutes a "valid price" (it must be a stand-alone word like $100, not part of another word like abc$100) and handling floating-point precision for the output.
This problem follows the String Tokenization and Validation pattern.
Sentence: "there are $100 and $50 items", Discount: 10%
"there are $90.00 and $45.00 items"$100abc as a price. A price must be a separate word.%.2f or toFixed(2), leading to answers like $90.1 instead of $90.10.Get comfortable with your language's string formatting and tokenization tools. In an interview, being able to quickly split a string and validate parts using built-in methods (like isdigit() or parseFloat) saves valuable time.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Length of the Longest Alphabetical Continuous Substring | Medium | Solve | |
| String Compression III | Medium | Solve | |
| Count and Say | Medium | Solve | |
| Validate IP Address | Medium | Solve | |
| String to Integer (atoi) | Medium | Solve |