KatInterview

Simulated Stock Trading Signals and Two-Char String Reduction

Stacks & QueuesModel answer
MediumModel answerStacks & Queuesstacks-queuesarraysstrings

Last updated

Solve two streaming state problems: daily stock trading based on sign patterns in O(N) time and two-character substring elimination using an in-place stack.

This multi-part problem tests your ability to process streaming data and maintain efficient state updates under strict time and memory bounds.


Task 1: Stock Trading by Sign Pattern Matching

You are given an integer array prices of size $N$ representing daily stock prices. You are also given two arrays of non-zero integers: buy_pattern (length $K_b$) and sell_pattern (length $K_s$). Both patterns consist exclusively of 1 (indicating a price increase from the previous day) and -1 (indicating a price decrease).

  1. Define the direction of price change on day $t$ ($t \ge 1$) as: $$\text{sign}(prices[t] - prices[t-1]) = \begin{cases} 1 & \text{if } prices[t] > prices[t-1] \ -1 & \text{if } prices[t] < prices[t-1] \ 0 & \text{if } prices[t] == prices[t-1] \end{cases}$$
  2. A pattern of length $k$ matches at day $t$ if the last $k$ daily signs $[\text{sign}(prices[t-k+1] - prices[t-k]), \dots, \text{sign}(prices[t] - prices[t-1])]$ are identical to the pattern. Note: A sign of 0 (flat price) breaks any potential match, as patterns contain only 1 and -1.
  3. You start on day 0 with 0 shares. On each day $t \ge 1$:
    • Check for a sell_pattern match. If matched and your current holdings are $> 0$, sell 1 share.
    • Check for a buy_pattern match. If matched, buy 1 share.
    • If both patterns match on the same day, process the sell operation first, followed by the buy operation.
    • Short selling is forbidden; holdings can never drop below 0.

Return an integer array holdings of length $N$, where holdings[t] is the number of shares held at the end of day $t$.

Target Complexity: $O(N)$ time and $O(1)$ extra space (excluding the output array).


Task 2: Iterative Removal of Forbidden Two-Character Pairs

You are given a string s and a set forbidden_pairs containing disallowed two-character combinations of lowercase English letters (e.g., {"ab", "bc", "cd"}).

Repeatedly delete any occurrence of any two-character pair in forbidden_pairs from s until no such pairs remain.

  1. Write a function that returns the length of the resulting string.
  2. Explain how to return the final string itself.
  3. Discuss the space and time complexity, aiming for $O(N)$ time and $O(1)$ extra space beyond an in-place buffer (or $O(\min(N, \Sigma))$ auxiliary space, where $\Sigma$ is the alphabet size).

Constraints:

  • $N \le 2 \times 10^5$
  • forbidden_pairs contains at most $26 \times 26 = 676$ unique pairs.

Hints

Hint 1

For Task 1: Re-evaluating the full pattern array of length K every day takes O(N * K) time. Can you maintain a rolling window of recent signs using a bitmask, along with a counter for consecutive non-zero diffs?

Hint 2

For Task 1: Remember to process sells before buys on the same day, and ensure holdings never go below zero.

Hint 3

For Task 2: Standard left-to-right greedy elimination can be implemented with a stack. How can you perform this stack operation in O(1) extra space if the input string is mutable?