Rolling Time-Series Rank Operator in Logarithmic Time

Last updated: November 29, 2026

Quick Overview

For a daily series x and window d, return for each day the rank of today's value among the valid values in the last d days, scaled to [0, 1] with ties counted half, in O(n log n) total time.

Worldquant
Coding & Algorithms
Software Engineer
Worldquant
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


For a daily series x and window d, return for each day the rank of today's value among the valid values in the last d days, scaled to [0, 1] with ties counted half, in O(n log n) total time.

A time-series rank operator is a building block of many BRAIN-style alpha expressions and gets evaluated for every instrument over decades, with windows up to a year. The onsite checks whether the candidate can go beyond sorting each window and pick a data structure that supports insert, delete and rank queries quickly, while matching the exact tie and NaN semantics.

What the Interviewer Expects
  • States exact semantics: count strictly less plus half of other ties, divided by valid count minus 1
  • Rejects re-sorting each window, which is O(n d log d)
  • Uses coordinate compression with a Fenwick tree, or an order statistic tree
  • Handles NaN by not inserting it and returning NaN for a NaN today
  • Validates against a brute force on random inputs with many ties
Key Topics to Cover
Fenwick tree
Coordinate compression
Sliding window
Order statistics
Streaming algorithms
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you run this over 5,000 instruments in parallel?
  • Could you do it in O(n log d) memory-wise without compressing the whole series up front, for streaming data?
  • How would you compute a rolling median with the same structure?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Output at t (for t at least d-1, and x[t] valid): #{v<xt}+12#{v=xt, v≠self}k−1\frac{\#\{v<x_t\} + \tfrac12\#\{v=x_t,\ v\ne\text{self}\}}{k-1} over valid values in the window, k being the valid count. If k is 1, return 0.5. NaN...

High-Level Design

Map every distinct valid value to an index 1..m (coordinate compression). A Fenwick tree over those indices stores how many window values sit at each index. Each step adds today's value, removes the v...


Submit Your Answer
Markdown supported

Related Questions