PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Affirm

Implement an LRU cache

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to design and implement efficient cache eviction policies while managing time and space complexity guarantees for mutable key-value stores.

  • easy
  • Affirm
  • Coding & Algorithms
  • Software Engineer

Implement an LRU cache

Company: Affirm

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Onsite

## Problem Design and implement an **LRU (Least Recently Used) cache** that supports the following operations in **average O(1)** time: - `get(key) -> value`: Return the value associated with `key` if it exists in the cache; otherwise return `-1`. Accessing a key makes it **most recently used**. - `put(key, value)`: Insert or update the value for `key`. If inserting causes the number of keys to exceed the cache `capacity`, evict the **least recently used** key. ## Input/Output You will implement a class (or module) with: - Constructor: `LRUCache(capacity)` - Methods: `get(key)` and `put(key, value)` ## Constraints (typical) - `1 <= capacity <= 10^5` - Keys and values are integers (or comparable scalar types) - Must achieve **O(1)** average time for both operations and **O(capacity)** space ## Notes - “Most recently used” means most recently accessed via `get` or updated via `put`. - Updating an existing key counts as a recent use.

Quick Answer: This question evaluates a candidate's ability to design and implement efficient cache eviction policies while managing time and space complexity guarantees for mutable key-value stores.

Related Interview Questions

  • Determine Redeemable Promotion Offers - Affirm (medium)
  • Compute Available Offers per User - Affirm (easy)
  • Aggregate loans and match repayments - Affirm (medium)
  • Implement a timestamped map - Affirm (medium)
  • Detect fraud events and extract PII - Affirm (medium)
Affirm logo
Affirm
Nov 6, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
7
0

Problem

Design and implement an LRU (Least Recently Used) cache that supports the following operations in average O(1) time:

  • get(key) -> value : Return the value associated with key if it exists in the cache; otherwise return -1 . Accessing a key makes it most recently used .
  • put(key, value) : Insert or update the value for key . If inserting causes the number of keys to exceed the cache capacity , evict the least recently used key.

Input/Output

You will implement a class (or module) with:

  • Constructor: LRUCache(capacity)
  • Methods: get(key) and put(key, value)

Constraints (typical)

  • 1 <= capacity <= 10^5
  • Keys and values are integers (or comparable scalar types)
  • Must achieve O(1) average time for both operations and O(capacity) space

Notes

  • “Most recently used” means most recently accessed via get or updated via put .
  • Updating an existing key counts as a recent use.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

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