ProgrammingHardCapgemini · TCS NQT
Each event is (time, +1) for a train arriving or (time, -1) for a train departing. Arrivals are at 1, 3, 3, 5, 6, 8 and departures are at 4, 4, 7, 9, 10, 11 (one per train). The events are sorted by time and a running count is kept: cur = cur + 1 for an arrival, cur = cur - 1 for a departure, and best = max(best, cur) after each event. What is printed as best?
Each event is (time, +1) for a train arriving or (time, -1) for a train departing. Arrivals are at 1, 3, 3, 5, 6, 8 and departures are at 4, 4, 7, 9, 10, 11 (one per train). The events are sorted by time and a running count is kept: cur = cur + 1 for an arrival, cur = cur - 1 for a departure, and best = max(best, cur) after each event. What is printed as best?
events sorted by time, departures before arrivals at equal time cur = 0; best = 0 for each event: update cur, then best = max(best, cur) Print best
- A. 6
- B. 3
- C. 12
- D. 0
Correct answer: B. 3
The count peaks at 3 just before the first departure at time 4, since arrivals at 1, 3 and 3 are all present. Counting arrivals alone gives 6, treating every event as an arrival gives 12, and never updating the best gives 0.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Five numbers 3, 30, 34, 5, 9 are to be arranged to form the largest possible number. Two numbers x, y are put in the order that gives the…
- Prices by day: 1, 5, 3, 8, 6, 10. Profit is earned in every rise: whenever tomorrow's price is higher than today's, buy today and sell…
- Array a = [3, 1, 2, 5, 4] is sorted by bubble sort: in each pass adjacent pairs out of order are swapped (a[j] > a[j+1]). What is the…
- Items of weights 7, 3, 5, 2, 6 are considered in increasing weight order. Each item is taken if it still fits in the capacity 13. What is…
- For i from 0 to 30 inclusive, count the values of i with ii <= 30. What is printed? count = 0 for i = 0 to 30 if i i <= 30 then count =…
- Array [4, 1, 3, 2] is sorted by selection sort: for each position i, find the minimum of the unsorted part and swap it into position i…