Peak CPU Usage in Every Sliding Window of Instruments Samples

Last updated: November 29, 2026

Quick Overview

Xcode Instruments records CPU usage samples at fixed intervals. Given the samples and a window size k, return the maximum usage in every contiguous window of k samples, in O(n) total time.

Apple
Coding & Algorithms
Software Engineer
Apple
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Xcode Instruments records CPU usage samples at fixed intervals. Given the samples and a window size k, return the maximum usage in every contiguous window of k samples, in O(n) total time.

Performance tooling at Apple summarizes long traces into windowed peaks, so this question tests the monotonic deque. The interviewer checks that you store indices, evict out-of-window entries from the front and dominated entries from the back.

What the Interviewer Expects
  • Start from the O(nk) brute force and the O(n log k) heap
  • Keep a deque of indices with decreasing sample values
  • Pop from the back while the new sample is at least as large
  • Pop from the front when the index leaves the window
  • Argue amortized O(n) because each index is pushed and popped once
Key Topics to Cover
monotonic queue
sliding window
deque
amortized analysis
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
  • Return the window minimum as well, in the same pass.
  • Samples arrive live; emit each window max as soon as it is final.
  • Windows are defined by time (last 500 ms) and samples are irregular.
  • Compute the windowed median instead of max.
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

Input: list samples of numbers and int k with 1 <= k <= len(samples). Output: list of len(samples) - k + 1 maxima. Empty input returns an empty list....

High-Level Design

Maintain a deque of indices whose sample values are strictly decreasing from front to back. When a new sample arrives, pop back indices with values less than or equal to it, since they can never be a ...


Submit Your Answer
Markdown supported