Solve four design-leaning coding problems
TwitchSoftware Engineer · OnsiteLast updated
This multi-part question evaluates algorithmic problem-solving, data structure design and manipulation (such as LRU cache semantics), stateful simulation and scoring logic, chronological quota accounting for billing, and set/map operations for detecting overlaps.
You are given four independent interview-style coding tasks. For each task, implement the requested function/class with clear inputs/outputs and reasonable constraints. (You do not need to integrate the tasks together.)
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 - 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. - 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:
LRUCache(capacity)get(key) -> value | -1(returns-1if not found)put(key, value)
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.
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]: integer array wherecounts[i]is the number of notifications sent during houri.freeLimit: total number of notifications per day that are free.unitPrice: 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: 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-up questions
- How would you optimize if you need to answer many queries for different `targetChannel` values?
- What is the time/space complexity of your approach?
- Constraints (suggested): up to `1e5` channels, total follower memberships up to `1e6`.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.