PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Oracle

Implement an LRU cache

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to design and implement efficient data structures that meet strict O(1) time constraints while managing access-based eviction policies, testing competencies in algorithmic reasoning, state management, and performance trade-offs.

  • medium
  • Oracle
  • Coding & Algorithms
  • Software Engineer

Implement an LRU cache

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design and implement an in-memory cache that evicts entries using the **Least Recently Used (LRU)** policy. The cache should store key–value pairs and support the following operations: - `LRUCache(int capacity)`: Initialize the cache with a positive integer `capacity` representing the maximum number of key–value pairs it can hold. - `int get(int key)`: - If the key exists in the cache, return its value and mark this key as **most recently used**. - If the key does not exist, return `-1`. - `void put(int key, int value)`: - Insert or update the value of the given key. - When inserting a new key causes the number of items to exceed `capacity`, you must **evict exactly one key–value pair: the one that has been least recently used** (i.e., the one whose `get` or `put` was accessed furthest in the past). - Updating an existing key should also mark it as **most recently used**. **Requirements:** - All `get` and `put` operations must run in **O(1)** average time. - You may assume: - Keys and values are integers. - `capacity >= 1`. Describe the data structures you would use and then implement the LRU cache in a language of your choice.

Quick Answer: This question evaluates a candidate's ability to design and implement efficient data structures that meet strict O(1) time constraints while managing access-based eviction policies, testing competencies in algorithmic reasoning, state management, and performance trade-offs.

Related Interview Questions

  • Solve Five Coding Problems - Oracle (medium)
  • Compute letter frequencies from encoded string - Oracle (medium)
  • Count closed islands in a grid - Oracle (easy)
  • Implement in-memory data structures and booking API - Oracle (hard)
  • Implement an LRU cache - Oracle (medium)
Oracle logo
Oracle
Nov 6, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
2
0

Design and implement an in-memory cache that evicts entries using the Least Recently Used (LRU) policy.

The cache should store key–value pairs and support the following operations:

  • LRUCache(int capacity) : Initialize the cache with a positive integer capacity representing the maximum number of key–value pairs it can hold.
  • int get(int key) :
    • If the key exists in the cache, return its value and mark this key as most recently used .
    • If the key does not exist, return -1 .
  • void put(int key, int value) :
    • Insert or update the value of the given key.
    • When inserting a new key causes the number of items to exceed capacity , you must evict exactly one key–value pair: the one that has been least recently used (i.e., the one whose get or put was accessed furthest in the past).
    • Updating an existing key should also mark it as most recently used .

Requirements:

  • All get and put operations must run in O(1) average time.
  • You may assume:
    • Keys and values are integers.
    • capacity >= 1 .

Describe the data structures you would use and then implement the LRU cache in a language of your choice.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Oracle•More Software Engineer•Oracle Software Engineer•Oracle 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.