PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Snapchat

Implement a size-bounded LRU cache

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to design and implement efficient data structures for capacity-bound caching, focusing on handling variable-sized items, eviction policies, and maintaining O(1) average get/put invariants.

  • hard
  • Snapchat
  • Coding & Algorithms
  • Software Engineer

Implement a size-bounded LRU cache

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Design and implement an LRU (Least Recently Used) cache where the cache capacity is measured by **total size**, not by item count. Each cached item has a variable `size` (positive integer). The cache has a maximum capacity `maxSize`. ## Operations Implement the following operations: 1. `get(key) -> value | null` - Return the value if `key` exists, else return `null`. - If the key exists, mark it as **most recently used**. 2. `put(key, value, size)` - Insert or update an item with the given `size`. - Mark the item as **most recently used**. - If inserting/updating causes total cached size to exceed `maxSize`, evict **one or more** least-recently-used items until `totalSize <= maxSize`. ## Requirements / Clarifications - `size` is a positive integer. - If `size > maxSize`, define and document expected behavior (commonly: do not store the item at all). - Updating an existing key may change its size; eviction may be needed. - Target time complexity: `O(1)` average time for `get` and `put` (excluding time spent evicting multiple items). ## Example `maxSize = 10` - `put(A, ..., size=6)` → total=6 - `put(B, ..., size=5)` → total would be 11, so evict LRU items until total<=10 (likely evict `A` if it is LRU) Describe data structures and invariants you would maintain.

Quick Answer: This question evaluates a candidate's ability to design and implement efficient data structures for capacity-bound caching, focusing on handling variable-sized items, eviction policies, and maintaining O(1) average get/put invariants.

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
Jan 2, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
2
0
Loading...

Design and implement an LRU (Least Recently Used) cache where the cache capacity is measured by total size, not by item count.

Each cached item has a variable size (positive integer). The cache has a maximum capacity maxSize.

Operations

Implement the following operations:

  1. get(key) -> value | null
    • Return the value if key exists, else return null .
    • If the key exists, mark it as most recently used .
  2. put(key, value, size)
    • Insert or update an item with the given size .
    • Mark the item as most recently used .
    • If inserting/updating causes total cached size to exceed maxSize , evict one or more least-recently-used items until totalSize <= maxSize .

Requirements / Clarifications

  • size is a positive integer.
  • If size > maxSize , define and document expected behavior (commonly: do not store the item at all).
  • Updating an existing key may change its size; eviction may be needed.
  • Target time complexity: O(1) average time for get and put (excluding time spent evicting multiple items).

Example

maxSize = 10

  • put(A, ..., size=6) → total=6
  • put(B, ..., size=5) → total would be 11, so evict LRU items until total<=10 (likely evict A if it is LRU)

Describe data structures and invariants you would maintain.

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.