FastPrepLRU Cache
Problems/LRU Cache

LRU Cache

Salesforce logoSalesforce● MediumFULLTIMENEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Salesforce onsite interview. The report requires an LRU Cache with O(1) operations, using a hash map and a doubly linked list. It does not give how a batch of commands is passed in, the value limits, or edge-case examples. Our version is about 91% the same as the reported question.

Process operations on a least recently used cache with fixed positive capacity. The cache starts empty.

  • put has arguments [key, value]. Insert or update the key and make it most recently used. If a new key makes the cache exceed capacity, evict the least recently used key. A put produces no result.
  • get has arguments [key]. Return its value and make the key most recently used, or return -1 when the key is absent.

Return the results of all get operations in operation order.

Function

runLruCache(capacity: int, operations: String[], arguments: int[][]) → int[]

Examples

Example 1

capacity = 2operations = ["put","put","get","put","get","get"]arguments = [[1,1],[2,2],[1],[3,3],[2],[3]]return = [1,-1,3]

Reading key 1 makes it recent. Inserting key 3 then evicts key 2.

Example 2

capacity = 1operations = ["put","put","get","put","get"]arguments = [[5,10],[5,20],[5],[6,30],[5]]return = [20,-1]

Updating key 5 changes its value without eviction. Inserting key 6 later evicts key 5.

Example 3

capacity = 2operations = ["get","put","get","get"]arguments = [[7],[7,70],[7],[7]]return = [-1,70,70]

The first read misses. After insertion, repeated successful reads return 70 and preserve recency.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= operations.length = arguments.length <= 2 * 10^5
  • Each operation is exactly get or put.
  • A get row has one integer; a put row has two integers.
  • 0 <= key, value <= 10^9
  • Every operation must run in average O(1) time.

More Salesforce Onsite Interview questions

  • Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
  • Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
  • 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
  • Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
  • Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
  • Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
  • Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
  • Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
  • LRU CacheLotusFlare · ONSITE INTERVIEW
  • LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
  • LRU Cache OperationsHevo Data · ONSITE INTERVIEW
  • LRU Cache OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • LRU Cache Operation SequenceGoogle · ONSITE INTERVIEWGoogle interview questions
  • Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
  • LRU CacheAMD · ONSITE INTERVIEWAMD interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public int[] runLruCache(int capacity, String[] operations, int[][] arguments) {
        // write your code here
    }
}
Checking account…