PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's competency in implementing a sliding-window expiring counter, testing knowledge of streaming algorithms, data structures for time-series aggregation, amortized time complexity, and memory-efficient design in the Coding & Algorithms domain.

  • medium
  • Uber
  • Coding & Algorithms
  • Software Engineer

Implement an expiring counter class

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Expiring Counter (sliding time window) Design and implement an **expiring counter** that counts events in a recent time window. ### Requirements - You are given a fixed window size `W` in **seconds** when constructing the counter. - Support two operations: 1. `inc(timestamp, delta=1)`: record `delta` events occurring at integer time `timestamp` (in seconds). 2. `get(timestamp) -> int`: return the total number of events recorded in the **inclusive** time range: \[ [timestamp - W + 1,\; timestamp] \] ### Assumptions / clarifications (state and use in your solution) - Timestamps are integer seconds. - You may assume calls arrive with **non-decreasing timestamps** (typical for streaming counters). If you choose not to assume this, explain how you would handle out-of-order inputs. ### Constraints / goals - Aim for efficient time and space usage: roughly **O(1) amortized** per operation and memory proportional to the window. ### Follow-up If `inc()` can be called up to **1,000,000 times per second**, how would you modify the design to reduce memory and CPU overhead (e.g., batching/aggregating within each second)?

Quick Answer: This question evaluates a candidate's competency in implementing a sliding-window expiring counter, testing knowledge of streaming algorithms, data structures for time-series aggregation, amortized time complexity, and memory-efficient design in the Coding & Algorithms domain.

Process inc/get operations for an inclusive sliding window counter. Calls arrive with non-decreasing timestamps; return outputs for get operations and None for inc.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: (3, [['inc',1,2], ['inc',2,1], ['get',3], ['get',4]])

Expected Output: [None, None, 3, 1]

Explanation: Inclusive 3-second window.

Input: (2, [['get',0], ['inc',5,4], ['get',6]])

Expected Output: [0, None, 4]

Explanation: Empty then one bucket.

Input: (1, [['inc',1], ['inc',1,3], ['get',1], ['get',2]])

Expected Output: [None, None, 4, 0]

Explanation: Aggregate same-second events.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Thread-Safe Token-Bucket Rate Limiter - Uber
  • Quadtree for 2D Geospatial Points - Uber
  • Group Anagrams - Uber
  • Deep Equality of Two Records - Uber (medium)
  • Shortest Path in a Grid with Blocked Cells - Uber (medium)