Interview Questions/Coding/Stock Span Ledger

Stock Span Ledger

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Stock Span Ledger

A trader records the price of one stock for several consecutive days. For each day, calculate its span. The span of a day is the number of consecutive days ending on that day for which the stock price was less than or equal to today's price. Return the span for every day.

Examples

Example 1
Input:
100 80 60 70 60 75 85
Output:
1 1 1 2 1 4 6

Approach hint

For each day, find the nearest previous day with a strictly greater price.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp