PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to design and implement a thread-safe per-user API rate limiter, focusing on concurrency control, synchronization techniques, and time-based request accounting in the Coding & Algorithms domain.

  • medium
  • Snapchat
  • Coding & Algorithms
  • Software Engineer

Implement a thread-safe rate limiter

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Design and implement a per-user API rate limiter. Requirements: - Method: `boolean allow(String userId, long nowMillis)` returns whether the request is allowed at time `nowMillis`. - Policy: allow at most **N** requests per **W** milliseconds per user. - Choose any standard algorithm (e.g., fixed window, sliding window, token bucket). Follow-ups: - How do you make it safe under concurrency (multiple threads calling `allow` for the same `userId`)? - Discuss tradeoffs between using locks vs lock-free/low-lock approaches (e.g., `ConcurrentHashMap`, atomics) and performance implications.

Quick Answer: This question evaluates a candidate's ability to design and implement a thread-safe per-user API rate limiter, focusing on concurrency control, synchronization techniques, and time-based request accounting in the Coding & Algorithms domain.

For each (user,time) request, allow at most limit requests per user in the previous window_ms interval.

Constraints

  • requests are processed in arrival order

Examples

Input: ([('u', 0), ('u', 10), ('u', 20)], 2, 100)

Expected Output: [True, True, False]

Explanation: Third rejected.

Input: ([('u', 0), ('v', 1), ('u', 100)], 2, 100)

Expected Output: [True, True, True]

Explanation: Boundary evicts old request.

Input: ([], 2, 100)

Expected Output: []

Explanation: No requests.

Hints

  1. Use one deque of accepted timestamps per user.
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

  • 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 Timestamped Counter - Snapchat (medium)