Preparing for an interview? Check out Cracking the Coding Interview
Published on

Amazon OA

Author
  • Shared Anonymously

Recently given amazon oa test, 2 coding questions has been asked one of them is based on greedy algorithm.
Faced difficulty with other question, below is the description.

Count the number of k-spikes in the stock price array ,which are counted as k-spikes

A k-spike is an element that satisfies both the following conditions

  1. There are atleast k elements from indices (0,i-1) that are less than prices[i]
  2. There are atleast k elements from indices (i+1,n-1) that are less than prices[i]

Example
prices=[1,2,8,5,3,4]
k=2

output - 2(elements 8 and 5 are 2-spikes)

Tried out brute force approach and memoization approach ,time limit has exceeded for few cases.
Tried using min heap to store [value,index] and checking for that index if the number of indices < index are >=k and number of indices >index are >=k, even then time limit exceeded for few cases.

Can anyone suggest better approach!

ReportMark as Helpful