Find Optimal Input
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's December 2024 OA is asking you to find optimal input, and the greedy pattern is your signal. This is a constraint-satisfaction problem where you're looking for the best choice at each step, but the trick is knowing when greedy actually works and when it's a trap. You'll need to prove that local optimization leads to global optimality, or you'll be chasing your tail. StealthCoder can validate your approach live if you're unsure mid-OA.
Pattern and pitfall
Greedy problems at Google almost always hinge on one thing: sorting or ordering first, then making the best immediate choice. The pattern here is find the invariant that justifies why picking the max (or min, or earliest, or lowest-cost) option now doesn't poison future choices. Common pitfall is assuming greedy works without proof. You'll want to think about exchange arguments: if you swapped your choice for any other, would the result get worse. Write out a small example by hand first, verify the greedy choice actually wins. If you blank on proof during the OA, StealthCoder will show you the working solution so you can code it clean.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Find Optimal Input 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as assign cookies. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Optimal Input FAQ
Is this a 'sort then greedily pick' problem or something weirder?+
Almost certainly sort first. Google loves greedy problems where ordering transforms the problem into obvious choices. Read the constraints and goal carefully. If you're picking items or scheduling or selecting intervals, sort by a key attribute and try picking greedily by that order.
How do I know if greedy is actually optimal here?+
Try a counterexample. Take your greedy algorithm on a small input by hand. If you find a case where greedy loses, it's not optimal. If you can't break it in 5 minutes, outline why swapping any choice for another would be worse. That's your proof sketch.
What if the problem doesn't say 'optimal' explicitly?+
Read the goal again. 'Minimize', 'maximize', 'best', 'most', 'fewest' all mean you need an optimal solution. Greedy is the hint because it's fast and elegant. Other patterns like DP would be overkill if greedy works.
Should I code greedy first or think through it completely?+
Think for 2-3 minutes. Sketch the algorithm on paper or a comment. If it feels solid, code it. If you're uncertain, consider a DP fallback. But greedy problems usually have clean solutions, so trust the pattern hint.
Does Google care about the proof, or just the right answer?+
They care about the right answer and clean code. If you're in an interview follow-up, explaining why greedy is optimal will impress. During the OA, correctness is what matters. Your solution just has to pass test cases.