Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sliding Window Trading Volume

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Sliding Window Trading Volume. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Akuna Capital's market-data feed provides trades in chronological order. Given each trade's timestamp and volume, find the maximum total volume traded during any fixed-duration time window.

Use a sliding-window technique and minimize auxiliary memory. A window is half-open, [start, start + window_seconds), so a trade exactly at start + window_seconds is excluded. Because volumes are nonnegative, it is sufficient to evaluate windows whose start aligns with a trade timestamp.

Formal Specification

Implement max_window_volume(timestamps, volumes, window_seconds).

  • timestamps is a list of nondecreasing integers representing seconds.
  • volumes is a list of nonnegative integers, where volumes[i] belongs to timestamps[i].
  • window_seconds is a positive integer.
  • Return an integer representing the greatest total volume in any valid window.
  • If timestamps is empty, return 0.

Constraints

  • 0 <= len(timestamps) == len(volumes) <= 10^5
  • 0 <= timestamps[i] <= 10^9
  • 0 <= volumes[i] <= 10^6
  • timestamps is sorted in nondecreasing order
  • 1 <= window_seconds <= 10^9

Function Signature

def max_window_volume(timestamps, volumes, window_seconds):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output