ProgrammingVery hardInfosys · Cognizant
The same stack method is applied to the string 5317 with exactly 2 digits deleted, but now the aim is the LARGEST number. The procedure: while k > 0, the stack is not empty, and the top is smaller than the current digit, pop and decrement k. Push the digit. After the scan, remove the remaining k digits from the end. What is printed?
The same stack method is applied to the string 5317 with exactly 2 digits deleted, but now the aim is the LARGEST number. The procedure: while k > 0, the stack is not empty, and the top is smaller than the current digit, pop and decrement k. Push the digit. After the scan, remove the remaining k digits from the end. What is printed?
k = 2; stack = empty
for each digit c of the string
while k > 0 and stack not empty and top < c: pop; k = k - 1
push c
end for
remove the last k digits; print the stack- A. 17
- B. 53
- C. 57
- D. 75
Correct answer: C. 57
Popping the 3 and then the 1 when 7 arrives leaves 5 then 7, so the answer is 57. Deleting the first digits gives 17, deleting the last gives 53, and sorting the digits in descending order gives 75 without respecting positions.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Children have greed values 7, 8, 9, 10 and cookies have sizes 5, 6, 7, 8, 8. A child is content if the cookie given is at least his greed…
- Closed intervals (start, end): (1,4), (2,6), (8,10), (9,12), (11,13), (3,5). The greedy below places points so that every interval…
- Job durations (burst times) are 6, 8, 7, 3, 4 with all jobs arriving at time 0. Shortest-job-first runs them one after another in…
- A row of positions 1 to 20 is scanned in order. A position i is picked if no position has been picked yet, or if i minus the last picked…
- Array a = [2,3,1,1,4,2,1,3,1,1] (0-based). From index i you may jump forward at most a[i] positions. Starting at index 0, the greedy below…
- Tasks with the same letter must be separated by at least n = 2 idle or other slots. The task string is AAABBC. The greedy formula below…