Bars Until the Close Exceeds the Current Level
Last updated: November 29, 2026
Quick Overview
For each bar in a series of closing prices, return how many bars later a strictly higher close first occurs, or 0 if it never does.
Voleon
November 29, 20260
0
0 solved
For each bar in a series of closing prices, return how many bars later a strictly higher close first occurs, or 0 if it never does.
This is an online-assessment style problem that Voleon could pose to check fluency with monotonic stacks on time-series data, the kind of building block used for breakout-style features. Graders look for the O(n) stack solution and correct handling of equal prices.
What the Interviewer Expects
- Recognize the next-greater-element pattern
- Keep a stack of indices still waiting for a higher close
- Use strict comparison so equal closes do not resolve each other
- Explain the amortized O(n) bound
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you compute bars since the last higher close instead?
- What if you need the first close at least 1% higher?
- How would this run incrementally on a live feed?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of closes. Output: list of the same length where ans[i] = j - i for the smallest j > i with close[j] > close[i], else 0....
High-Level Design
A brute-force scan forward from every bar is O(n^2). Instead, sweep left to right holding a stack of indices whose answer is still unknown; their closes are nonincreasing from bottom to top. When a ne...