Skip to the code
cf.
+10 XP

#152 First Sold-Out Product

easy

A shop's product grid shows items in a fixed order. stock[i] is how many units of the item in slot i are left. The grid should show a "Sold out" banner above the first empty slot.

Write findFirstEmpty(stock) that returns the index of the first slot whose count is 0, or -1 if nothing is sold out.

The counts are in no particular order.

Example 1

Input: stock = [5,2,0,7,0]
Output: 2

Slots 2 and 4 are empty. Slot 2 comes first.

Example 2

Input: stock = [3,1,4]
Output: -1

Every product still has stock.

Constraints

  • 0 <= stock.length <= 10000
  • 0 <= stock[i] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).