PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Snapchat

Implement a recent-use eviction cache

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to design and implement a fixed-capacity key-value cache with a recent-use eviction policy, emphasizing data structure design, state management, and meeting average O(1) time constraints for get and put operations.

  • medium
  • Snapchat
  • Coding & Algorithms
  • Software Engineer

Implement a recent-use eviction cache

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design a fixed-capacity key-value cache that removes the least recently accessed item when it becomes full. Implement a class with the following operations: - `Cache(int capacity)`: Initialize the cache with a positive capacity. - `int get(int key)`: Return the value associated with `key` if it exists; otherwise return `-1`. A successful `get` marks the key as the most recently used item. - `void put(int key, int value)`: Insert or update the value for `key`. If the cache is already at capacity and the key does not already exist, evict the least recently used key before inserting. Updating an existing key also marks it as the most recently used item. Requirements: - Both `get` and `put` should run in average `O(1)` time. - Keys and values are integers. Example behavior: 1. `Cache(2)` 2. `put(1, 10)` 3. `put(2, 20)` 4. `get(1)` returns `10` and makes key `1` most recently used. 5. `put(3, 30)` evicts key `2` because it is the least recently used. 6. `get(2)` returns `-1`.

Quick Answer: This question evaluates a candidate's ability to design and implement a fixed-capacity key-value cache with a recent-use eviction policy, emphasizing data structure design, state management, and meeting average O(1) time constraints for get and put operations.

Related Interview 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)
Snapchat logo
Snapchat
Sep 29, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
0
0
Loading...

Design a fixed-capacity key-value cache that removes the least recently accessed item when it becomes full.

Implement a class with the following operations:

  • Cache(int capacity) : Initialize the cache with a positive capacity.
  • int get(int key) : Return the value associated with key if it exists; otherwise return -1 . A successful get marks the key as the most recently used item.
  • void put(int key, int value) : Insert or update the value for key . If the cache is already at capacity and the key does not already exist, evict the least recently used key before inserting. Updating an existing key also marks it as the most recently used item.

Requirements:

  • Both get and put should run in average O(1) time.
  • Keys and values are integers.

Example behavior:

  1. Cache(2)
  2. put(1, 10)
  3. put(2, 20)
  4. get(1) returns 10 and makes key 1 most recently used.
  5. put(3, 30) evicts key 2 because it is the least recently used.
  6. get(2) returns -1 .

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Snapchat•More Software Engineer•Snapchat Software Engineer•Snapchat Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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.