Exchange Cups
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
TikTok's August 2024 OA threw this at candidates: Exchange Cups. No verbatim problem text available, but the pattern is simulation with a twist of greedy choice. You're likely swapping or rearranging cups under some constraint, and the gotcha is figuring out the stopping condition or the optimal swap order. This is the kind of problem where the first solution that comes to mind isn't always right. StealthCoder can help you catch the edge case live if you freeze on the logic.
Pattern and pitfall
Exchange Cups is almost certainly a simulation problem wrapped in greedy logic. You're given cups in some initial state, and you need to get them to a target state through exchanges. The trick: not all exchange sequences are equal. Some orderings reach the goal faster, some get stuck. The pitfall is assuming any valid swap is fine; it's not. You need to identify which exchanges actually reduce distance to the goal state, then apply them in order. If the problem allows you to exchange any two cups, you're doing a sorting-like operation. If it restricts which cups can swap, it becomes more like a graph problem where you find the shortest path. StealthCoder's real-time pattern matching during the OA means you won't second-guess whether you're overthinking the constraints.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Exchange Cups 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Exchange Cups FAQ
Is this a sorting problem in disguise?+
Not exactly. Sorting assumes you can swap any two elements freely. Exchange Cups probably restricts which cups can swap based on position or value. Read the constraints carefully. If swaps are unrestricted, it collapses to a greedy simulation. If restricted, you're doing graph traversal or BFS to find the minimum exchanges.
What's the most common mistake on this problem?+
Greedy swaps without tracking the goal state. Candidates pick the 'next best' exchange without checking if it actually gets closer to the target. Always maintain a distance metric. Use BFS if the state space is small, or greedy with validation if it's large.
How do I tell if I need BFS or greedy?+
BFS finds the minimum number of exchanges; greedy finds a valid sequence fast but not always optimal. Check the problem's objective. If it asks for minimum exchanges, BFS. If it just asks whether exchange is possible, greedy. TikTok likely wants the minimum path since it's algorithmic.
Should I pre-compute all possible states?+
Only if the state space is small (cups <= 10). Otherwise you'll time out. Use BFS with early termination when you hit the goal, or use greedy heuristics. Pay attention to memory limits in the OA.
What if the problem has no solution?+
Some cup configurations can't be reached from others by allowed exchanges. You need to detect this, usually by checking reachability via BFS or by analyzing parity. The problem statement will hint at this. Don't assume every input has an answer.