FastPrepStock Buy Sell Position Indicator

Stock Buy Sell Position Indicator

Hudson River Trading logoHudson River Trading● MediumOA
Learn

Problem statement

You are given integer prices and two non-empty movement patterns, buyIndicator and sellIndicator. Each pattern value is 1 for an increase or -1 for a decrease.

Scan prices from left to right and keep the history of non-zero price movements. Equal adjacent prices add no movement and cannot trigger a signal, but they do not erase the earlier non-zero movement history. At each non-equal price, compare the end of that history with both indicators.

A buy match changes the position by +1; a sell match changes it by -1. Both may match at the same index, in which case their changes cancel. Return the cumulative position at every price index, starting with 0 at index 0.

Function

buySellStock(prices: int[], buyIndicator: int[], sellIndicator: int[]) → int[]

Examples

Example 1

prices = [51, 56, 56, 58, 60, 59, 54, 57, 52, 48]buyIndicator = [1, 1]sellIndicator = [-1, -1, 1]return = [0, 0, 0, 1, 2, 2, 2, 1, 1, 1]

Ignore the unchanged step from 56 to 56 when building the non-zero movement history. The rises ending at indices 3 and 4 each complete [1,1], and the movement ending at index 7 completes [-1,-1,1]. The cumulative positions are therefore [0,0,0,1,2,2,2,1,1,1].

Constraints

  • prices.length >= 1
  • buyIndicator.length >= 1
  • sellIndicator.length >= 1
  • Every indicator value is either 1 or -1.

More Hudson River Trading problems

See Hudson River Trading hiring insights
public int[] buySellStock(int[] prices, int[] buyIndicator, int[] sellIndicator) {
  // write your code here
}
  
prices[51, 56, 56, 58, 60, 59, 54, 57, 52, 48]
buyIndicator[1, 1]
sellIndicator[-1, -1, 1]
expected[0, 0, 0, 1, 2, 2, 2, 1, 1, 1]
Checking account…