PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/TikTok

Implement an LRU cache

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of cache design and eviction policies (LRU), along with competency in selecting and reasoning about data structures and complexity to support efficient get and put operations under capacity constraints.

  • easy
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Implement an LRU cache

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem: LRU Cache Design and implement a data structure that supports an **LRU (Least Recently Used) cache** with a fixed capacity. ### Requirements Implement a cache with the following operations: - `get(key) -> value` - If `key` exists in the cache, return its value and mark the entry as **most recently used**. - If `key` does not exist, return `-1`. - `put(key, value) -> void` - Insert or update the `(key, value)` pair. - If inserting causes the number of keys to exceed `capacity`, evict the **least recently used** entry. - Updating an existing key should also mark it as **most recently used**. ### Performance Constraints - Target time complexity: **O(1)** average for both `get` and `put`. - Space complexity: **O(capacity)**. ### Example Assume `capacity = 2`: - `put(1, 10)` - `put(2, 20)` - `get(1) -> 10` (now key `1` is most recently used) - `put(3, 30)` (evicts key `2` as least recently used) - `get(2) -> -1` ### Notes You may choose any programming language, but clearly describe the data structures you use and how you ensure O(1) operations.

Quick Answer: This question evaluates understanding of cache design and eviction policies (LRU), along with competency in selecting and reasoning about data structures and complexity to support efficient get and put operations under capacity constraints.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Dec 15, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
4
0

Problem: LRU Cache

Design and implement a data structure that supports an LRU (Least Recently Used) cache with a fixed capacity.

Requirements

Implement a cache with the following operations:

  • get(key) -> value
    • If key exists in the cache, return its value and mark the entry as most recently used .
    • If key does not exist, return -1 .
  • put(key, value) -> void
    • Insert or update the (key, value) pair.
    • If inserting causes the number of keys to exceed capacity , evict the least recently used entry.
    • Updating an existing key should also mark it as most recently used .

Performance Constraints

  • Target time complexity: O(1) average for both get and put .
  • Space complexity: O(capacity) .

Example

Assume capacity = 2:

  • put(1, 10)
  • put(2, 20)
  • get(1) -> 10 (now key 1 is most recently used)
  • put(3, 30) (evicts key 2 as least recently used)
  • get(2) -> -1

Notes

You may choose any programming language, but clearly describe the data structures you use and how you ensure O(1) operations.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

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

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