Skip to content

feat: add StockSpanProblem algorithm#7312

Open
oleksii-tumanov wants to merge 2 commits intoTheAlgorithms:masterfrom
oleksii-tumanov:stock-span-problem
Open

feat: add StockSpanProblem algorithm#7312
oleksii-tumanov wants to merge 2 commits intoTheAlgorithms:masterfrom
oleksii-tumanov:stock-span-problem

Conversation

@oleksii-tumanov
Copy link
Contributor

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

Adds a monotonic-stack implementation of the Stock Span Problem.

The implementation:

  • computes the span for each day in linear time
  • returns an array of spans for the input prices
  • validates null input

Closes #7311

Test Case

Validated with:

  • mixed prices: [10, 4, 5, 90, 120, 80] -> [1, 1, 2, 4, 5, 1]
  • increasing prices
  • decreasing prices
  • repeated equal prices
  • empty array
  • single-element array
  • null input throws IllegalArgumentException

Commands used:

  • mvn -Dtest=StockSpanProblemTest test
  • mvn -DskipTests checkstyle:check

@codecov-commenter
Copy link

codecov-commenter commented Mar 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.45%. Comparing base (7d57c57) to head (5c2e538).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7312      +/-   ##
============================================
+ Coverage     79.43%   79.45%   +0.02%     
- Complexity     7062     7070       +8     
============================================
  Files           788      789       +1     
  Lines         23124    23134      +10     
  Branches       4545     4548       +3     
============================================
+ Hits          18368    18381      +13     
+ Misses         4022     4021       -1     
+ Partials        734      732       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@oleksii-tumanov oleksii-tumanov marked this pull request as ready for review March 14, 2026 01:43
@AbhiramSakha
Copy link

Looks good. Consider adding comments for better readability.

@oleksii-tumanov
Copy link
Contributor Author

Looks good. Consider adding comments for better readability.

more comments added

@AbhiramSakha
Copy link

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

@oleksii-tumanov
Copy link
Contributor Author

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Can you check StockSpanProblem.java implementation please? I have comments for each code line already.
Do you have any suggestions what to add there?

@AbhiramSakha
Copy link

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Can you check StockSpanProblem.java implementation please? I have comments for each code line already. Do you have any suggestions what to add there?

The implementation looks clean and well-structured, and the inline comments are helpful for understanding the logic.

You might consider adding:

  • A brief explanation of the time complexity (O(n)) and why the stack approach is efficient
  • A short description of the problem statement at the top of the class for better context
  • An example input/output to help beginners quickly understand how the algorithm works

Overall, great work!

@oleksii-tumanov
Copy link
Contributor Author

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Can you check StockSpanProblem.java implementation please? I have comments for each code line already. Do you have any suggestions what to add there?

The implementation looks clean and well-structured, and the inline comments are helpful for understanding the logic.

You might consider adding:

  • A brief explanation of the time complexity (O(n)) and why the stack approach is efficient
  • A short description of the problem statement at the top of the class for better context
  • An example input/output to help beginners quickly understand how the algorithm works

Overall, great work!

Thank you Abhiram.
Added a brief explanation of the time complexity.

Other info like description and example already in place.

@AbhiramSakha
Copy link

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Looks good. Consider adding comments for better readability.

more comments added

Nice implementation. Adding inline comments would make it easier for beginners to understand the logic.

Can you check StockSpanProblem.java implementation please? I have comments for each code line already. Do you have any suggestions what to add there?

The implementation looks clean and well-structured, and the inline comments are helpful for understanding the logic.
You might consider adding:

  • A brief explanation of the time complexity (O(n)) and why the stack approach is efficient
  • A short description of the problem statement at the top of the class for better context
  • An example input/output to help beginners quickly understand how the algorithm works

Overall, great work!

Thank you Abhiram. Added a brief explanation of the time complexity.

Other info like description and example already in place.

Glad to hear that! The implementation looks great now. Thanks for the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Add Stock Span Problem algorithm in Java

3 participants