FastPrepFind First Index Where Prefix Sum Becomes Non-Positive
Problems/Find First Index Where Prefix Sum Becomes Non-Positive

Find First Index Where Prefix Sum Becomes Non-Positive

Amazon logoAmazon● EasyFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an array of integers inventory representing inventory stock. The prefix sum at index i is the cumulative sum inventory[0] + inventory[1] + ... + inventory[i]. Find the first (smallest) 0-based index i where the prefix sum is non-positive, that is, less than or equal to 0. If no such index exists, return -1.

Function

findFirstNonPositivePrefixSumIndex(inventory: int[]) → int

Complete the function findFirstNonPositivePrefixSumIndex in the editor.

findFirstNonPositivePrefixSumIndex has the following parameter:

  1. int[] inventory: an array of integers representing inventory stock

Returns int: the first 0-based index where the prefix sum is less than or equal to 0, or -1 if there is none

Examples

Example 1

inventory = [1, 2]return = -1

Prefix sums are [1, 3], no non-positive sum.

Example 2

inventory = [2, -4, 1]return = 1

Prefix sums are [2, -2, -1]. The running sum first drops to 0 or below at index 1 (2 + (-4) = -2), so the answer is 1.

Example 3

inventory = [1, 2, 3, -6]return = 3

Prefix sums are [1, 3, 6, 0], becomes non-positive at index 3.

Constraints

  • 1 <= n <= 10^5, where n is the length of inventory
  • -10^9 <= inventory[i] <= 10^9

More Amazon interview questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
  • Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
  • Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026

Similar questions at other companies

  • Minimum Positive Starting ValueBooking.com · OA
  • Array Interval SumsHeadlands Technologies · OAHeadlands Technologies interview questions
  • Minimum AmountBelvedere Trading · OA
  • Online No-Intercept Linear RegressionTwo Sigma · OATwo Sigma interview questions
  • Range Sum QueriesIntuit · OAIntuit interview questions
  • Count Sortable SplitsGoogle · OAGoogle interview questions
  • Array Challenge (QR Intern)Akuna Capital · OAAkuna Capital interview questions
  • Range Negation UpdatesJP Morgan Chase · OAJP Morgan Chase interview questions
  • Threshold AlertsGoldman Sachs · OAGoldman Sachs interview questions
  • First Prefix Reaching a Target ScoreVisa · OAVisa interview questions

Interview reports that mention this question

  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int findFirstNonPositivePrefixSumIndex(int[] inventory) {
      // write your code here
    }

}
Checking account…