Interview Experience + Machine Coding Round (Stock Price Volatility Monitor)
Recently had an interview where the first part focused on Kafka — producers, consumers, consumer groups, offset management, partitions, message ordering, backpressure handling, and Kafka exactly-once semantics.
After going deeper into Kafka internals, log compaction, retries, and idempotency, I was given a machine coding round.
Below is the question exactly as asked:
📌 Machine Coding Round: Stock Price Volatility Monitor
Problem Statement
You are given a continuous stream of stock price changes (both positive and negative).
Your task is to design a VolatilityMonitor class that keeps track of the maximum volatility within the last N trades.
👉 Volatility = absolute value of price change
You must implement two methods:
1️⃣ recordTrade(int priceChange)
Adds a new trade.
Only the last N trades are stored — so this forms a sliding window.
2️⃣ getMaxVolatility() → int
Returns the maximum absolute price change in the current sliding window.
📘 Example
Let tradeWindow = 3
Operation Window Max Volatility
recordTrade(5) [5] 5
recordTrade(-3) [5, -3] 5
recordTrade(8) [5, -3, 8] 8
recordTrade(2) [-3, 8, 2] 8
The window always slides to keep only the last N price changes.