Reported July 2025
Virtu Financialgreedy

Maximum Apples That Fit in a Box

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

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

Virtu Financial reportedly served this one in July 2025, and it looks harder than it is. Strip the box story and it's a sorting problem: you have a fixed remaining capacity, a pile of apple weights, and you want the most apples. That's greedy, pick the lightest first. If your nerves spike when you see a finance firm's name on the invite, relax. This is a few lines of code. If you blank on the live OA anyway, StealthCoder runs invisibly as a safety net and hands you the approach in real time.

The problem

A box can hold at most 5000 grams and may already contain some items. You want to add as many apples as possible without exceeding the box's capacity.
You are given a zero-indexed integer array a:
a[0] is the total weight, in grams, of the items already in the box.
For every i >= 1, a[i] is the weight, in grams, of one available apple.
Return the maximum number of available apples that can be added to the box without making its total weight exceed 5000 grams.

Function
maximumApples(a: int[]) → int

Examples
Example 1
a = [4650,150,150,150]
return = 2
The box already contains 4650 grams, leaving 350 grams of capacity. Two apples weighing 150 grams each fit, bringing the total to 4950 grams. A third apple would exceed the capacity, so the answer is 2.

Constraints
1 <= a.length <= 100
0 <= a[i] <= 5000

Reported by candidates. Source: FastPrep

Pattern and pitfall

Compute remaining capacity as 5000 minus a[0]. Sort the apples, which are a[1:], in ascending order. Walk through them, subtracting each weight from the remaining capacity, and count while it stays at or above zero. Stop at the first apple that doesn't fit, because every later apple is heavier or equal. The greedy proof is simple: swapping any chosen apple for a lighter unchosen one never hurts. Common pitfalls: including a[0] in the sorted apple list, using strict less-than so an exact fit of 5000 gets rejected, and forgetting the length-1 case where there are no apples and the answer is 0. Complexity is O(n log n) for the sort, which is trivial at n up to 100. Edge case: a[0] can be 5000, leaving zero capacity, so zero-weight apples still fit. StealthCoder is your hedge if the live OA rattles you, but the logic here is short enough to hold in your head.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Maximum Apples That Fit in a Box 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

Virtu Financial 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.

Maximum Apples That Fit in a Box FAQ

What's the trick in Maximum Apples That Fit in a Box?+

Sort the apple weights ascending and take the lightest ones first until the remaining capacity runs out. Remaining capacity is 5000 minus a[0]. Greedy works because lighter apples always leave more room, so no other selection can beat it for count.

How hard is this Virtu Financial OA question really?+

Easy. It's a disguised greedy plus sort problem with a tiny input size. The only difficulty is reading the setup correctly, since a[0] is existing box weight and not an apple. Most of the risk is off-by-one mistakes, not algorithm choice.

What edge cases should I test?+

Test an array of length 1 (no apples, answer 0), a[0] equal to 5000 (only zero-weight apples fit), an apple that fits exactly to 5000, and apples with weight 0. Also check that a[0] is excluded from the sorted list.

Is greedy-after-sorting still commonly asked in OAs?+

Yes. Sort-then-take-smallest shows up constantly in screening rounds because it's quick to write and easy to grade. Recognizing 'maximize count under a budget' as a sort-and-accumulate pattern covers a lot of similar questions.

How do I prepare for this in 48 hours?+

Write this solution once from memory in your chosen language. Then do two or three other 'maximize count under a limit' problems, like buying items with a budget. Practice reading the input format carefully, since the a[0] twist is where people slip.

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

OA at Virtu Financial?
Invisible during screen share
Get it