Largest Binary-String Subset Within Bit Budgets
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the trap is hiding in plain sight: the budgets can be 0, and duplicate strings count as separate picks. If you reach for greedy or plain DFS, the edge cases will eat you. This is Ones and Zeroes in disguise, a 2D knapsack with two capacities. You've got an OA invite and maybe two days. Know the shape of the solution cold. StealthCoder sits invisibly on your screen during the live assessment as a safety net if the DP state goes blank on you.
The problem
Given an array of binary strings strs and two budgets, maxOnes and maxZeroes, return the maximum number of strings you can select. The selected strings must contain at most maxOnes ones in total and at most maxZeroes zeroes in total. Each array position may be selected at most once, including when two positions contain equal strings. Function largestBoundedSubset(strs: String[], maxOnes: int, maxZeroes: int) → int Examples Example 1 strs = ["100","10","1","11","111"] maxOnes = 3 maxZeroes = 0 return = 2 Select "1" and "11". They use exactly three ones and no zeroes. Example 2 strs = ["10","0001","111001","1","0"] maxOnes = 3 maxZeroes = 5 return = 4 The strings "10", "0001", "1", and "0" use three ones and five zeroes. Example 3 strs = ["10","0","1"] maxOnes = 1 maxZeroes = 1 return = 2 Selecting "0" and "1" uses both budgets and yields two strings. Constraints 1 <= strs.length <= 600. 1 <= strs[i].length <= 100. Every strs[i] contains only 0 and 1. 0 <= maxOnes, maxZeroes <= 100.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Treat each string as an item with a cost of (zeroes, ones) and a value of 1. Build dp[z][o] as the max strings you can pick using at most z zeroes and o ones. For each string, count its zeroes and ones, then loop z from maxZeroes down to zeroes and o from maxOnes down to ones. Update dp[z][o] = max(dp[z][o], dp[z-zc][o-oc] + 1). The backward loop is the whole trick, because it stops you from using one position twice. The common pitfall is looping forward, which turns this into unbounded knapsack and breaks the at-most-once rule. Greedy by shortest string fails too, and DFS with no memo blows up at 600 strings. Also check that a string costing more than either budget is simply skipped, which the loop bounds handle. Complexity is O(n * maxZeroes * maxOnes). If you blank mid-assessment, StealthCoder is the hedge that hands you the loop order.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Largest Binary-String Subset Within Bit Budgets 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon 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.
Largest Binary-String Subset Within Bit Budgets FAQ
What's the trick to this Amazon OA problem?+
It's a 0/1 knapsack with two dimensions. Each string costs some zeroes and some ones, and is worth one point. Keep dp[z][o] as the best count under those budgets, and iterate both budgets downward so each string is used once.
Why does my forward loop give wrong answers?+
Looping budgets upward lets the same string get counted multiple times in one pass, because the updated cell feeds later cells. That's unbounded knapsack. Iterate z and o from the max down to the string's cost to keep every pick single-use.
Can greedy work here?+
No. Picking the shortest or cheapest strings first can burn one budget while the other sits unused. Example 3 shows it: you need to pair "0" and "1" across both budgets. Only DP considers every trade-off between zeroes and ones.
What edge cases should I test?+
Test both budgets at 0, a budget of 0 on only one side, duplicate strings that should count separately, and strings costing more than either budget. Also try a single-element array. The DP handles all of these if the loop bounds are correct.
How do I prepare in 48 hours?+
Write the 2D knapsack from scratch twice, then trace Example 2 by hand. Practice the backward loop until it's automatic. Know the complexity is O(n * maxZeroes * maxOnes), roughly 6 million operations at the limits, which is fine.