Count Later Heart Rate Readings Lower Than Each Reading
Last updated: November 29, 2026
Quick Overview
Given a sequence of resting heart rate readings, return for each index i how many readings after i are strictly smaller than readings[i]. The Health app uses this to flag days that were followed by many improvements.
Apple
November 29, 20260
0
0 solved
Given a sequence of resting heart rate readings, return for each index i how many readings after i are strictly smaller than readings[i]. The Health app uses this to flag days that were followed by many improvements.
An onsite question in the style of Apple Health data teams, testing ordered-set thinking beyond the quadratic scan. The interviewer expects a Fenwick tree, merge sort counting, or a balanced BST approach, with coordinate compression.
What the Interviewer Expects
- Start from the O(n^2) brute force and explain why it is too slow
- Process readings from right to left
- Coordinate compress values to ranks
- Use a Fenwick tree to count smaller values seen so far
- State O(n log n) time
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
- Count readings after i that are within 5 bpm of readings[i].
- Readings stream in and you must answer for the newest reading against all earlier ones.
- Solve it with a merge sort that tracks original indices.
- What changes if you need 'smaller or equal'?
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 ints readings, length n up to about 10^5. Output: list counts where counts[i] is the number of j > i with readings[j] < readings[i]....
High-Level Design
Scan from right to left so that, when we reach i, the structure holds exactly the readings after i. Compress values to ranks 1..m. A Fenwick tree over ranks supports add(rank) and prefix(rank) in ...