MEDIUMasked at 1 company

Number of Ways to Buy Pens and Pencils

A medium-tier problem at 56% community acceptance, tagged with Math, Enumeration. Reported in interviews at Reddit and 0 others.

Founder's read

You've got a budget, two prices, and infinite supply of both items. How many ways can you spend exactly that money or less. Sounds simple until you realize the brute force gets slow fast. Reddit asked this one. The trick isn't complex, but the optimization separates people who think in terms of O(n^2) from those who see O(n). If you hit this cold in your assessment and blank on the math pattern, StealthCoder runs invisibly during screen share and surfaces the working solution in seconds.

Companies asking
1
Difficulty
MEDIUM
Acceptance
56%

Companies that ask "Number of Ways to Buy Pens and Pencils"

If this hits your live OA

Number of Ways to Buy Pens and Pencils is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The obvious move is loop through all possible counts of item A, then loop through all possible counts of item B. That works but it's inefficient. The real pattern: fix the count of the expensive item (or cheaper, doesn't matter), then calculate how many ways the remaining budget can buy the second item. You don't need to enumerate every single combination. Instead, for each fixed quantity of one item, you derive the valid range for the other using division and subtraction. This drops you from O(n^2) to O(n). Common miss: people iterate both nested and never think 'wait, can I just math this.' StealthCoder handles the edge cases and closed-form counting that trip up live candidates.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Number of Ways to Buy Pens and Pencils recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Ways to Buy Pens and Pencils interview FAQ

How hard is this really for a MEDIUM?+

The acceptance rate sits at 56 percent, which is fair for MEDIUM. The brute force passes if your time limit is loose, but optimized O(n) is expected at top companies. The math insight isn't deep, but spotting it under pressure is the actual challenge. Reddit's known for precise implementations.

What's the trick I'm probably missing?+

You don't enumerate both loops. Fix one item's count, then use math (division) to count valid quantities of the other. That's it. Candidates usually loop both and feel confused when they realize it's too slow, then panic. See it once, apply it every time.

Is this still asked at companies like Reddit?+

Reddit has reported asking it. Smaller company count in the data, so it's not everywhere, but it appears as a screening problem where they care about both correctness and algorithm efficiency, not just a working solution.

How does Enumeration relate to this problem?+

Enumeration means counting. You're enumerating valid spending combinations. The bad way enumerates everything explicitly (nested loops). The good way enumerates implicitly using math. Both are enumeration strategies, one is just smarter.

Should I memorize the closed form or derive it live?+

Derive it. Write the nested loop first to confirm logic, then optimize by observing that the inner loop can be replaced with arithmetic. This shows your thinking and handles edge cases (negative budgets, zero prices) naturally without memorization tricks.

Want the actual problem statement? View "Number of Ways to Buy Pens and Pencils" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.