Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The array [7, 1, 5, 3, 6, 4] is processed to find the maximum profit from one buy and one later sell (0 if none). What is the maximum profit?
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
Track the lowest price so far and the best gain. The buy at 1 and sell at 6 gives the largest difference of 5.
More Coding Interview Problems questions
- Problem: A grid of 3×3 cells is numbered 1 to 9 row by row. Starting at 1 (top-left), the robot moves right or down. The number of paths…
- Problem: A list of intervals is [1,5], [2,3], [4,8], [7,9], [10,11]. Merge all overlapping intervals (touching at an endpoint counts as…
- Problem: The string "aabbbcccc" is encoded by writing each letter followed by its count, e.g. "a2b3c4". What is the length of the encoded…
- Problem: What is the number of inversions in the array [2, 5, 1, 4, 3]?
- Problem: The array [3, 7, 1, 9] is used to create all non-empty contiguous subarrays. How many of them have an odd sum?
- Problem: In the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], find the number of pairs (i, j) with i < j whose product is a multiple of 12.