ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
// LRU cache of capacity 2: get(k) returns the value or -1; put evicts the least recently used key when full
put(1, 1)
put(2, 2)
Print get(1)
put(3, 3)
Print get(2)
put(4, 4)
Print get(1), get(3), get(4)
  1. A. 1 2 -1 3 4
  2. B. 1 -1 -1 3 4
  3. C. 1 -1 1 -1 4
  4. D. 1 -1 1 3 -1

Correct answer: B. 1 -1 -1 3 4

get(1) returns 1 and makes key 1 recent, so put(3) evicts 2: get(2) = −1. put(4) then evicts the least recent key, 1 (3 was used more recently): get(1) = −1, get(3) = 3, get(4) = 4. Output 1 -1 -1 3 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic