Skip to content
Preptima
mediumCodingEntryMidSenior

Find the largest sum obtainable from a contiguous run of elements in an array.

4 min readUpdated 2026-08-15Target archetype: Big Tech, Product Startup

Answer

Short answer

Walk the array once, keeping the best sum ending at the current element: either that element alone, or that element added to the best run ending before it. The maximum over those running values is the maximum subarray sum, and the version that resets the running sum to zero is wrong when every element is negative.

Was this answer helpful?

Likely follow-ups

  • Return the start and end indices of the best run. Where exactly do you record the start?
  • The array is split across four machines. What does each one have to send you?
  • Allow at most one element to be dropped from the run. How does the state change?
  • What if the array is enormous and arrives as a stream you cannot revisit?

Related questions