I have this question asked in the 4th round of my AWS onsite and failed to solve it. Can anybody want to give a try ?
Question:
Given a sequence of numbers N, the elements are in increasing & decreasing sequence. Find the top k (k max) elements from the sequence.
Note: The interviewer was expecting this to be solved using Two Pointers (with O(n) TC). Not using heaps or sorting it.
Example 1:
Input:
N = 14
21, 23, 24, 25, 6, 5, 4, 2, 11, 13, 17, 19, 100, 99
K = 3
Output:
100, 99, 25
Example 2:
Input:
N = 8
9, 5, 1, 10, 22, 19, 17, 10
K = 4
Output:
22, 19, 17, 10