1. Environment-Specific Logging Without Code Changes
In a scenario where you have two environments, production (prod) and deployment, with identical codebases, how can you achieve a setup where only the deployment environment writes logs and the production environment does not, without adding any environment check logic in the code and ensuring logs are written to the same log file?
2. System Design for a Trading Platform
Design a trading platform with a focus on a stock buy/sell matching system. The system should include two queues, one for sell orders and one for buy orders, and should be able to match orders appropriately.
3. N-Queens II
The problem is based on the N-Queens puzzle where you have to place n queens on an n×n chessboard such that no two queens attack each other. The task is to return the number of distinct solutions to the n-queens puzzle.
4. Find the N-th Largest Stock Price in a Stream
Assume there is a stream that continuously receives stock prices. Given two variables N and K, print out the N-th largest stock price when the X-th data (0 <= X <= N) is inserted into the stream.
Example:
Stream of stock prices: [1, 2, 3, 4, 5, 6]
N is 6, K is 3
The result should be: [-1, -1, 1, 2, 3, 4]
Steps:
Step 1: [1] -> 3rd largest does not exist, return -1
Step 2: [1, 2] -> 3rd largest does not exist, return -1
Step 3: [1, 2, 3] -> 3rd largest is 1, return 1
Step 4: [1, 2, 3, 4] -> 3rd largest is 2, return 2
Step 5: [1, 2, 3, 4, 5] -> 3rd largest is 3, return 3
Step 6: [1, 2, 3, 4, 5, 6] -> 3rd largest is 4, return 4
Discuss your approach to finding a solution with the smallest time complexity and space complexity, and how you would implement the loop to handle the stream of data.