Profit Analysis

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

The Virtu Financial OA reported in July 2026 hands you a profit array with up to 200,000 months and asks for the best contiguous stretch of at most k months. Checking every start and every length is O(n*k), which dies at this input size. This is a prefix sum problem with a sliding window minimum on top. If the monotonic deque part slips your mind mid-assessment, StealthCoder sits invisible on your screen as a safety net and gives you the working solution.

The problem

The profit and loss for each month is represented by an integer array pnl. A positive value represents profit earned in that month, while a negative value represents a loss.
Return the maximum net profit obtainable from any non-empty contiguous segment of months whose length is at most k.

Function
getMaxProfit(pnl: int[], k: int) → long

Examples
Example 1
pnl = [-3,4,3,-2,2,5]
k = 4
return = 8
The segment [3,-2,2,5] has length 4 and total profit 3 + (-2) + 2 + 5 = 8. The segment [4,3,-2,2,5] has total profit 12, but its length is 5, which exceeds k = 4.

Constraints
1 <= pnl.length <= 2 * 10^5
-10^9 <= pnl[i] <= 10^9
1 <= k <= pnl.length

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build prefix sums P where P[i] is the sum of the first i months. A segment ending at month j with length at most k has sum P[j+1] - P[i], where i ranges from j+1-k to j. So for each end, you want the minimum prefix value in a sliding window of size k. Use a monotonic deque of indices with increasing prefix values. Pop from the back while the new value is smaller, pop from the front when the index falls out of the window. Total time is O(n). Pitfalls: values reach 10^9 across 2*10^5 months, so use 64-bit integers. The segment must be non-empty, so the window for i must stop at j, never j+1. Don't seed the answer with 0, since all-negative input must return the least bad single month. StealthCoder is your hedge if the deque boundaries blur on the live OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Profit Analysis 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Profit Analysis FAQ

What's the trick in Virtu Financial's Profit Analysis?+

Convert to prefix sums, then for each end index find the smallest prefix in the last k positions. That's a sliding window minimum, solved with a monotonic deque in O(n). Plain Kadane's algorithm ignores the length cap, so it gives wrong answers like 12 instead of 8 on the example.

Why does brute force fail here?+

With n up to 2*10^5 and k up to n, checking every start and length is roughly n*k operations, which can reach 4*10^10. That's far too slow. You need O(n) or O(n log n), which means the prefix sum plus deque approach.

What happens if every month is a loss?+

The segment must be non-empty, so you return the largest single negative value, or the best short segment. Don't initialize your answer to 0. Start with negative infinity, or the first computed candidate, and take the max as you go.

Do I need a long type?+

Yes. A segment can sum up to 2*10^5 times 10^9, which is 2*10^14. That overflows a 32-bit int. Use long for the prefix array and the answer. The function signature already returns long, which hints at this.

How do I prepare for this in 48 hours?+

Practice two things: building prefix sums and writing a monotonic deque for a sliding window minimum. Then test your code on the sample, an all-negative array, k equal to 1, and k equal to n. Those four cases catch nearly every off-by-one bug in this problem.

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