Reported August 2024
TikTokgreedy

Get Optimal Content Storage

Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live TikTok OA. Under 2s to a working solution.
Founder's read

TikTok's August OA included a greedy optimization problem about content storage. You've got a set of items with different sizes and values, and you need to pick which ones to store under a capacity constraint to maximize value. It's the knapsack pattern with a greedy twist. Most candidates overthink it or try dynamic programming when a greedy sort solves it cleanly. StealthCoder will catch you if the greedy approach isn't obvious in the first read.

Pattern and pitfall

The trick is sorting by value-to-size ratio (or just value per unit capacity) and greedily picking items in that order until you run out of space. The common mistake is sorting by value alone or by size alone, which leaves money on the table. Greedy works here because you're maximizing density, not solving the 0-1 knapsack problem. If the problem says you can take fractional items or multiple copies, greedy is near-guaranteed. If it's strictly 0-1 knapsack with small bounds, you might need DP, but TikTok usually signals that differently. Test your sort order with a small example before submitting. StealthCoder can verify your greedy comparator instantly if you freeze mid-implementation.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Get Optimal Content Storage cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass TikTok's OA.

TikTok reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Get Optimal Content Storage FAQ

Is this a knapsack problem or a greedy problem?+

If you can take partial items or the capacity is large relative to item count, it's greedy. If it's strictly take-or-skip and the numbers are small enough for DP (capacity under 10000), it might be 0-1 knapsack. The hint says greedy, so sort by value/size ratio and pick in order.

What's the pitfall most people hit?+

Sorting by value alone and ignoring size, or vice versa. You need the ratio. A high-value, huge-size item might not fit and wastes your budget. A small, decent-value item fills the tank efficiently. Always compute value per unit of capacity.

Can I solve this in 48 hours if I've never seen it?+

Yes. Recognize the pattern from the hint, code a simple sort, and iterate. Test with 2-3 examples by hand. Don't overthink. If your first sort doesn't work, flip to the inverse ratio. You'll nail it in under 20 minutes.

Should I use a heap or just sort?+

Sort once and iterate. Heaps are overkill here unless you're doing repeated insertions and queries, which isn't the case for a one-shot greedy pick. A single sort and a for loop is cleaner and faster.

What if I blank on the ratio formula during the OA?+

Write down value / size on paper first. Then code it. If your first attempt fails, swap numerator and denominator. One will be correct. StealthCoder will have your back if you genuinely freeze and need the exact formula.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with TikTok.

OA at TikTok?
Invisible during screen share
Get it