ProgrammingHardCAT · MBA placement
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 tomorrow. The procedure sums all such daily rises. What is printed?
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 tomorrow. The procedure sums all such daily rises. What is printed?
total = 0
for i = 0 to n - 2
if p[i+1] > p[i] then total = total + p[i+1] - p[i]
end for
Print total- A. 13
- B. 9
- C. 4
- D. 3
Correct answer: A. 13
The rises are 4, 5 and 4 for a total of 13. The overall range of 9 ignores the intermediate falls, summing the falls gives 4 instead, and counting rises gives 3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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…
- Sorted array a = [1, 2, 3, 5, 8, 9] and limit L = 10. Count the pairs of distinct positions i < j with a[i] + a[j] <= L using two…
- The string s = "aaabbbcc" is cut down so that no two adjacent letters are equal, by deleting characters. The greedy keeps a character when…