This question evaluates understanding of ordered maps, iteration semantics, reverse traversal and accumulation of values in code. It is commonly asked in Coding & Algorithms interviews to test the ability to reason about iteration order, sentinel return values and summation behavior, and it examines practical application of data-structure semantics rather than purely abstract theory.
Assume you have a data structure global_var[a][time] that behaves like an ordered map:
time
) are kept in
sorted ascending
order.
order(map, key, dir)
returns the
next key
after
key
.
dir = 1
, it returns the next higher key.
dir = -1
, it returns the next lower key.
key = ""
, then
order(map, "", 1)
returns the
smallest
key and
order(map, "", -1)
returns the
largest
key.
""
.
Given:
global_var["A"] = {
2: 5,
4: 6,
9: 1,
15: 3
}
And this pseudocode:
time = ""
sum = 0
visited = []
while true:
time = order(global_var["A"], time, -1)
if time == "":
break
visited.append(time)
sum = sum + global_var["A"][time]
print(visited)
print(sum)
Task: What does the program print (the visited list and sum)?