PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of data structures and algorithmic optimization for constant-time cache operations, with emphasis on cache eviction policies and state management.

  • medium
  • Disney
  • Coding & Algorithms
  • Software Engineer

Implement an LRU cache

Company: Disney

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## LRU Cache Design and implement a fixed-capacity **Least Recently Used (LRU)** cache. ### Operations - `get(key) -> value | -1/null`: Return the value if present; otherwise indicate missing. Accessing a key makes it **most recently used**. - `put(key, value)`: Insert or update the value. Updating also makes it **most recently used**. ### Eviction rule - When inserting causes the cache to exceed capacity, evict the **least recently used** item. ### Requirements - Target time complexity: `O(1)` average for both `get` and `put`. ### Input/Output format You may choose an interface/class-based implementation (common in interviews). Clearly define how missing keys are represented (e.g., `-1` for integers).

Quick Answer: This question evaluates understanding of data structures and algorithmic optimization for constant-time cache operations, with emphasis on cache eviction policies and state management.

Implement solution(capacity, operations). operations contains ["put", key, value] and ["get", key]. Return the values produced by get operations, using -1 for missing keys. get and put must update recency; inserting beyond capacity evicts the least recently used key.

Constraints

  • 0 <= capacity <= 10^5
  • operations length <= 2 * 10^5
  • keys and values are integers

Examples

Input: (2, [['put', 1, 1], ['put', 2, 2], ['get', 1], ['put', 3, 3], ['get', 2], ['put', 4, 4], ['get', 1], ['get', 3], ['get', 4]])

Expected Output: [1, -1, -1, 3, 4]

Input: (1, [['put', 5, 10], ['put', 5, 11], ['get', 5], ['put', 6, 12], ['get', 5], ['get', 6]])

Expected Output: [11, -1, 12]

Input: (0, [['put', 1, 1], ['get', 1]])

Expected Output: [-1]

Hints

  1. Use a hash map plus a recency order structure.
  2. Updating an existing key also makes it most recently used.
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

  • Generate All Valid Combinations of Balanced Parentheses - Disney (medium)
  • Generate All Well-Formed Parenthesis Strings - Disney (medium)
  • Filter ads by a single rule - Disney (medium)
  • Determine if chasing points will meet - Disney (medium)
  • Solve matrix add, frequency count, longest consecutive - Disney (Medium)