PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to design and implement time-indexed counting data structures, reason about strictly increasing timestamps and query semantics, and consider performance and scalability trade-offs.

  • medium
  • Snapchat
  • Coding & Algorithms
  • Software Engineer

Implement a Timestamped Counter

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a data structure that records calls to an `increment` method. Requirements: - Each call to `increment(timestamp)` receives an integer `timestamp`. - Timestamps are strictly increasing across calls. - Implement `query(t)` to return how many times `increment` was called with a timestamp greater than `t`. Example: - Calls: `increment(1)`, `increment(3)`, `increment(3)`, `increment(10)` - `query(3)` should return `1` if `query` means strictly after `3`, or `1` plus any later duplicates depending on the exact interpretation. Make your API behavior explicit. Follow-up discussion: 1. How would you support a very high call rate, where `increment` may be invoked many times per second? 2. How would you optimize query performance further? 3. If the requirements were extended to support counting calls in any time interval `[start, end]`, what data structures would you consider?

Quick Answer: This question evaluates the ability to design and implement time-indexed counting data structures, reason about strictly increasing timestamps and query semantics, and consider performance and scalability trade-offs.

Record increment timestamps and answer counts strictly after t, plus optional inclusive range counts.

Constraints

  • Increment timestamps are non-decreasing

Examples

Input: ((('increment', 1), ('increment', 3), ('increment', 3), ('increment', 10), ('query', 3)),)

Expected Output: [1]

Explanation: Strictly greater than 3 returns one.

Input: ((('query', 5),),)

Expected Output: [0]

Explanation: No increments yet.

Input: ((('increment', 1), ('increment', 2), ('increment', 5), ('range', 2, 5)),)

Expected Output: [2]

Explanation: Inclusive range count.

Hints

  1. Store timestamps in sorted order and binary-search query boundaries.
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
  • 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

  • Determine Whether Courses Can Be Completed - Snapchat (medium)
  • Solve Decimal Coin Change - Snapchat (medium)
  • Find Maximum Island Perimeter - Snapchat (medium)
  • Solve Three Algorithmic Tasks - Snapchat (hard)
  • Implement a custom list with iterator and map - Snapchat (medium)