FastPrepStock Price Range over a Sliding Day Window

Stock Price Range over a Sliding Day Window

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Return maximum minus minimum among the most recent k prices in stockPrices. If k exceeds the number of prices, use the full history. Return -1 for empty input.

Function

stockPriceRange(stockPrices: int[], k: int) → int

Examples

Example 1

stockPrices = [4,1,8,2]k = 2return = 6

The latest prices are 8 and 2.

Constraints

  • k > 0.
  • At most 10^5 prices.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int stockPriceRange(int[] stockPrices, int k) {
  // Write your code here.
}
stockPrices[4,1,8,2]
k2
expected6
Checking account…