PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Citadel

Implement LRU/LFU cache with custom eviction

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in cache design and data-structure implementation, focusing on eviction policies (LRU, LFU), metadata tracking for recency and frequency, and the ability to support an extensible eviction interface in the Coding & Algorithms domain.

  • easy
  • Citadel
  • Coding & Algorithms
  • Software Engineer

Implement LRU/LFU cache with custom eviction

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are asked to implement an in-memory key-value cache with a fixed capacity. When the cache is full and a new item must be inserted, an eviction policy decides which existing item to remove. ## Part A — LRU Cache Implement an **LRU (Least Recently Used)** cache supporting the operations below in **O(1)** average time: - `get(key) -> value | -1` : Return the value for `key` if present, otherwise `-1`. A successful `get` counts as a “use”. - `put(key, value) -> void` : Insert or update the key. A `put` of an existing key counts as a “use”. If insertion causes the cache to exceed `capacity`, evict the **least recently used** key. ## Part B — LFU Cache Implement an **LFU (Least Frequently Used)** cache supporting the same `get/put` operations. Eviction rules: - Evict the key with the **lowest access frequency**. - If multiple keys tie on frequency, evict the **least recently used among those tied keys**. - Target **O(1)** average time for `get/put`. ## Part C — Custom Eviction Function Extend the cache design so that eviction behavior can be customized. For example, the cache may be constructed with something like: - `evict_fn(keys, metadata) -> key_to_evict` Requirements: - The cache must still support `get/put`. - When full, it must call the provided eviction function (or policy object) to select a key to remove. - Clearly define what metadata is tracked (e.g., recency, frequency, timestamps) and how it is updated. ## Clarifications / Constraints - Keys are hashable (e.g., integers or strings) and values are arbitrary objects. - `capacity` is a positive integer. - If `capacity == 0`, all `put` operations store nothing and all `get` return `-1`. - State any additional assumptions you need.

Quick Answer: This question evaluates proficiency in cache design and data-structure implementation, focusing on eviction policies (LRU, LFU), metadata tracking for recency and frequency, and the ability to support an extensible eviction interface in the Coding & Algorithms domain.

Related Interview Questions

  • Implement a single-producer multi-consumer ring buffer - Citadel (medium)
  • Sort a Nearly Sorted Array - Citadel (hard)
  • Compute BBO and NBBO from order data - Citadel (medium)
  • Design dynamic weighted random sampling with updates - Citadel (medium)
  • Compute maximum later-earlier difference - Citadel (medium)
Citadel logo
Citadel
Feb 11, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
20
0
Loading...

You are asked to implement an in-memory key-value cache with a fixed capacity. When the cache is full and a new item must be inserted, an eviction policy decides which existing item to remove.

Part A — LRU Cache

Implement an LRU (Least Recently Used) cache supporting the operations below in O(1) average time:

  • get(key) -> value | -1 : Return the value for key if present, otherwise -1 . A successful get counts as a “use”.
  • put(key, value) -> void : Insert or update the key. A put of an existing key counts as a “use”. If insertion causes the cache to exceed capacity , evict the least recently used key.

Part B — LFU Cache

Implement an LFU (Least Frequently Used) cache supporting the same get/put operations. Eviction rules:

  • Evict the key with the lowest access frequency .
  • If multiple keys tie on frequency, evict the least recently used among those tied keys .
  • Target O(1) average time for get/put .

Part C — Custom Eviction Function

Extend the cache design so that eviction behavior can be customized. For example, the cache may be constructed with something like:

  • evict_fn(keys, metadata) -> key_to_evict

Requirements:

  • The cache must still support get/put .
  • When full, it must call the provided eviction function (or policy object) to select a key to remove.
  • Clearly define what metadata is tracked (e.g., recency, frequency, timestamps) and how it is updated.

Clarifications / Constraints

  • Keys are hashable (e.g., integers or strings) and values are arbitrary objects.
  • capacity is a positive integer.
  • If capacity == 0 , all put operations store nothing and all get return -1 .
  • State any additional assumptions you need.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

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