PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to implement a constant-time LRU cache and reason about thread-safety and concurrent access, testing data structure design and concurrency competencies in the Coding & Algorithms domain.

  • Medium
  • Pinterest
  • Coding & Algorithms
  • Data Scientist

Implement LRUCache with O(1) Operations and Thread Safety

Company: Pinterest

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

##### Scenario High-traffic API needs constant-time eviction cache. ##### Question Implement an LRUCache supporting get(key) and put(key,val) in O( 1). Describe thread-safety considerations for concurrent access. ##### Hints Combine hash map with doubly-linked list; lock striping or concurrent linked hash map for threads.

Quick Answer: This question evaluates a candidate's ability to implement a constant-time LRU cache and reason about thread-safety and concurrent access, testing data structure design and concurrency competencies in the Coding & Algorithms domain.

Implement lru_ops(ops, capacity) to simulate an LRU cache with the given capacity. Each operation is either ["put", key, value] or ["get", key]. get(key) returns the value for key or -1 if absent, and marks the key as most-recently-used. put(key, value) inserts or updates key, marking it most-recently-used; if capacity is exceeded, evict the least-recently-used key. Return a list of integers containing the results of get operations in order. Assume single-threaded execution for testing.

Constraints

  • 1 <= capacity <= 100000
  • 0 <= len(ops) <= 200000
  • Each op is ["get", key] or ["put", key, value]
  • Keys and values are 32-bit signed integers
  • Average O(1) time per operation
  • Space O(capacity)

Hints

  1. Combine a hash map (key -> node) with a doubly linked list to track recency.
  2. Move a node to the list head on every get/put; evict from the tail.
  3. For thread-safety in real systems, guard the cache with a single mutex; for higher concurrency, consider a read-write lock or segmenting (lock striping) while keeping list and map updates atomic.
Last updated: Mar 29, 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
  • AI Coding 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

  • First Word Matching Each Prefix Query - Pinterest (medium)
  • Hierarchical Access Control for an Advertising Platform - Pinterest (medium)
  • Maximize Boxes Stored Through One Entrance - Pinterest (medium)
  • Solve Multiple Coding Interview Problems - Pinterest (medium)
  • Implement a Sparse Matrix Class - Pinterest (medium)