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)
- A. 1 2 -1 3 4
- B. 1 -1 -1 3 4
- C. 1 -1 1 -1 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer g[3][4] = {{1, 1, 1, 0}, {1, 1, 1, 1}, {0, 1, 1, 1}} Integer c = 0 for (each…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function count7(Integer n) if…
- What will be the output of the following pseudocode? String s = "ABCAB" Integer cnt[26] = all 0, total = 0 for (each ch in s) cnt[ch] =…
- What will be the output of the following pseudocode? String s = "COMPUTER" Integer i = 0, j = length(s) - 1 while (i < j) while (i < j AND…
- What will be the output of the following pseudocode? Function isSub(String a, String b) // is b a subsequence of a? Integer j = 0 for…
- What will be the output of the following pseudocode? String s = "ABBACA" Stack st = empty for (each ch in s) if (st not empty AND top(st)…