FastPrepLRU Cache with Thread-Safety Follow-Up
Problems/LRU Cache with Thread-Safety Follow-Up

LRU Cache with Thread-Safety Follow-Up

Snap Inc. logoSnap Inc.● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Snap Inc. phone screen. The report keeps the LRU cache task, the stress on edge cases, and the follow-up about thread safety. It does not give a function signature, examples or limits. Our version is about 90% the same as the reported question.

Process a sequence of operations on a least recently used cache with capacity capacity.

  • PUT key value inserts or updates a key. An update makes the key most recently used. If a new insertion exceeds capacity, evict the least recently used key.
  • GET key returns the value, or -1 when absent. A hit makes the key most recently used; a miss does not change recency.

Return all GET results in operation order. Operation fields are decimal integers separated by one space.

Function

runLRU(capacity: int, operations: String[]) → int[]

Examples

Example 1

capacity = 2operations = ["PUT 1 10","PUT 2 20","GET 1","PUT 3 30","GET 2","GET 3"]return = [10,-1,30]

The lookup of key 1 makes key 2 least recently used, so inserting key 3 evicts key 2.

Example 2

capacity = 1operations = ["PUT 5 7","PUT 5 9","GET 5","PUT 6 4","GET 5"]return = [9,-1]

Updating key 5 replaces its value without adding a second entry. Inserting key 6 later evicts it.

Example 3

capacity = 3operations = ["GET 8","PUT 8 0","GET 8"]return = [-1,0]

The first lookup misses. After insertion, a stored value of 0 is returned normally.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= operations.length <= 2 * 10^5
  • Every operation is exactly GET key or PUT key value.
  • -10^9 <= key, value <= 10^9

More Snap Inc. interview questions

  • ID Allocator with Assign and ReleasePHONE SCREEN · Seen Nov 2026
  • Basic Calculator IIPHONE SCREEN · Seen Oct 2026
  • Stream Cluster Max and MedianPHONE SCREEN · Seen Oct 2026
  • Smallest Range Covering Elements from K ListsPHONE SCREEN · Seen Sep 2026
  • Most Stones Removed with Same Row or ColumnPHONE SCREEN · Seen Sep 2026
  • Report Island Areas in Discovery OrderPHONE SCREEN · Seen Aug 2026
  • Render an Org Chart and Find Skip-Level PairsONSITE INTERVIEW · Seen Nov 2026
  • Peak of an Increasing-Then-Decreasing ArrayONSITE INTERVIEW · Seen Oct 2026
  • Number of Closed IslandsONSITE INTERVIEW · Seen Oct 2026
  • Radar Barrier CrossingONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • LRU CacheAthenahealth · PHONE SCREEN
  • LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
  • LRU Key-Value Cache OperationsCohesity · PHONE SCREEN
  • Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
  • Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
  • LRU CacheCoupang · PHONE SCREEN
  • LRU CacheBlinkit · PHONE SCREENBlinkit interview questions
  • Least Recently Used CacheIllumio · PHONE SCREENIllumio interview questions

Snap Inc. interview experiences

  • Snap Experienced MLE Onsite Interview Questions: 1 Experience
  • Snap AI Coding Interview, Experienced SWE and MLE: 4 Experiences
  • Snap Experienced MLE Virtual Onsite: 1 Experience
All Snap Inc. interview questionsSee Snap Inc. hiring insights
Saved
class Solution {
    public int[] runLRU(int capacity, String[] operations) {
      // write your code here
    }
}
Checking account…