Solve four design-leaning coding problems
1) Rock–Paper–Scissors Scoring with Bonuses
Design a small game engine that computes the score for a sequence of Rock–Paper–Scissors rounds.
Rules
- Each round has two moves:
playerMoveandopponentMove, each in{R, P, S}. - Outcome:
- Win: player gets
+1point - Tie: player gets
0points - Loss: player gets
0points
- Win: player gets
- Bonus A (Win Streak): If the player wins two or more rounds in a row, then each win after the first consecutive win gets an extra +1.
- Example: outcomes
W, W, Wyields base1+1+1plus streak bonus0+1+1.
- Example: outcomes
- Bonus B (Break a Tie): If the previous round was a tie and the current round is a win, the current round gets an extra +1.
Requirements
Given a list of rounds in chronological order, return:
- The total score for the whole sequence.
- The score earned per round (after bonuses).
Input/Output
- Input:
rounds = [(playerMove1, oppMove1), ..., (playerMoveN, oppMoveN)] - Output:
(totalScore, perRoundScores)
Constraints: 1 <= N <= 2e5.
2) LRU Cache API
Implement an in-memory cache with Least Recently Used (LRU) eviction.
Requirements
Implement a class with the following methods:
LRUCache(capacity): Initializes the cache with a given maximumcapacity.get(key) -> value | -1: Returns thevalueassociated withkey. Ifkeyis not found, returns-1. A successfulgetmakes that key the most recently used.put(key, value): Inserts or updates thevalueforkey. Ifkeyalready exists, its value is updated, and it becomes the most recently used. If the cache exceedscapacityafter insertion, the least recently used key is evicted.
Rules:
- When the cache exceeds
capacity, evict the least recently used key. - Any successful
getmakes that key the most recently used. putof an existing key updates its value and makes it most recently used.- Target complexity:
O(1)average time per operation forgetandput.
Follow-up (Thread Safety)
Describe how you would make this cache safe under concurrent access from multiple threads (you do not need to implement concurrency primitives unless explicitly asked).
3) 24-Hour Notification Billing with a Daily Free Quota
A notification service charges based on the number of notifications sent each hour of a day (24 hours total).
Inputs
counts[24]: An integer array wherecounts[i]is the number of notifications sent during houri.freeLimit: The total number of notifications per day that are free.unitPrice: The cost per notification beyond the free limit.
Billing Rule
- The
freeLimitis applied chronologically from hour0to hour23(first-come-first-served). - For each hour, some portion of that hour’s notifications may be free until the daily
freeLimitis exhausted.
Output
Return:
totalCostfor the day.hourlyCost[24]wherehourlyCost[i]is the cost charged for houri.
Constraints: 0 <= counts[i] <= 1e9, 0 <= freeLimit <= 1e12, 0 <= unitPrice <= 1e6.
4) Find Channels with Overlapping Followers
You are given a mapping from channels to their followers.
Input
channelToFollowers: A map/dictionary wherechannelToFollowers[channel]is a list (or set) of follower IDs.targetChannel: A channel name that exists in the map.
Task
Return all other channels that share at least one follower with targetChannel.
Output
Return a list of channels (excluding targetChannel) that overlap.
Follow-ups
- How would you optimize if you need to answer many queries for different
targetChannelvalues? - What is the time/space complexity of your approach?
Constraints (suggested): up to 1e5 channels, total follower memberships up to 1e6.
AI interview prep
Core dilemma
Balancing O(1) average-case time complexity for cache operations (get, put, eviction) with maintaining recency order and ensuring thread safety under concurrent access.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve four design-leaning coding problems
1) Rock–Paper–Scissors Scoring with Bonuses
Design a small game engine that computes the score for a sequence of Rock–Paper–Scissors rounds.
Rules
- Each round has two moves:
playerMoveandopponentMove, each in{R, P, S}. - Outcome:
- Win: player gets
+1point - Tie: player gets
0points - Loss: player gets
0points
- Win: player gets
- Bonus A (Win Streak): If the player wins two or more rounds in a row, then each win after the first consecutive win gets an extra +1.
- Example: outcomes
W, W, Wyields base1+1+1plus streak bonus0+1+1.
- Example: outcomes
- Bonus B (Break a Tie): If the previous round was a tie and the current round is a win, the current round gets an extra +1.
Requirements
Given a list of rounds in chronological order, return:
- The total score for the whole sequence.
- The score earned per round (after bonuses).
Input/Output
- Input:
rounds = [(playerMove1, oppMove1), ..., (playerMoveN, oppMoveN)] - Output:
(totalScore, perRoundScores)
Constraints: 1 <= N <= 2e5.
2) LRU Cache API
Implement an in-memory cache with Least Recently Used (LRU) eviction.
Requirements
Implement a class with the following methods:
LRUCache(capacity): Initializes the cache with a given maximumcapacity.get(key) -> value | -1: Returns thevalueassociated withkey. Ifkeyis not found, returns-1. A successfulgetmakes that key the most recently used.put(key, value): Inserts or updates thevalueforkey. Ifkeyalready exists, its value is updated, and it becomes the most recently used. If the cache exceedscapacityafter insertion, the least recently used key is evicted.
Rules:
- When the cache exceeds
capacity, evict the least recently used key. - Any successful
getmakes that key the most recently used. putof an existing key updates its value and makes it most recently used.- Target complexity:
O(1)average time per operation forgetandput.
Follow-up (Thread Safety)
Describe how you would make this cache safe under concurrent access from multiple threads (you do not need to implement concurrency primitives unless explicitly asked).
3) 24-Hour Notification Billing with a Daily Free Quota
A notification service charges based on the number of notifications sent each hour of a day (24 hours total).
Inputs
counts[24]: An integer array wherecounts[i]is the number of notifications sent during houri.freeLimit: The total number of notifications per day that are free.unitPrice: The cost per notification beyond the free limit.
Billing Rule
- The
freeLimitis applied chronologically from hour0to hour23(first-come-first-served). - For each hour, some portion of that hour’s notifications may be free until the daily
freeLimitis exhausted.
Output
Return:
totalCostfor the day.hourlyCost[24]wherehourlyCost[i]is the cost charged for houri.
Constraints: 0 <= counts[i] <= 1e9, 0 <= freeLimit <= 1e12, 0 <= unitPrice <= 1e6.
4) Find Channels with Overlapping Followers
You are given a mapping from channels to their followers.
Input
channelToFollowers: A map/dictionary wherechannelToFollowers[channel]is a list (or set) of follower IDs.targetChannel: A channel name that exists in the map.
Task
Return all other channels that share at least one follower with targetChannel.
Output
Return a list of channels (excluding targetChannel) that overlap.
Follow-ups
- How would you optimize if you need to answer many queries for different
targetChannelvalues? - What is the time/space complexity of your approach?
Constraints (suggested): up to 1e5 channels, total follower memberships up to 1e6.