Skip to content

[FEATURE REQUEST] Add Stock Span Problem algorithm in Java #7311

@oleksii-tumanov

Description

@oleksii-tumanov

What would you like to Propose?

I want to add an implementation of the Stock Span Problem to the Java repository.

The Stock Span Problem is a classic monotonic-stack algorithm that computes, for each day, how many consecutive previous days had a stock price less than or equal to the current day's price. The implementation runs in linear time using a stack of indices.

Issue details

Expected behavior:
Given an array of stock prices, the algorithm should return an array where each position contains the span for that day.

Example:

  • prices: [100, 80, 60, 70, 60, 75, 85]
  • expected result: [1, 1, 1, 2, 1, 4, 6]

Planned/implemented test coverage:

  • mixed example arrays
  • increasing prices
  • decreasing prices
  • repeated equal prices
  • empty array
  • single-element array
  • null input throws IllegalArgumentException

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions