Recently Watched Videos
Updated
Problem statement
You are building the Recently watched row of a video streaming app. The row lists videos from the most recently watched to the least recently watched. Each video appears in the row at most once, and the row holds at most capacity videos.
Run the operations in operations in order. Each operation is one of these:
["watch", videoId]: the user watchesvideoId. The video moves to the front of the row. If it was already in the row, it is moved, not added a second time. If the row now holds more thancapacityvideos, remove the video at the end of the row, which is the least recently watched one.["recent"]: report the row as it is now, from the front to the end.
Return one list for each "recent" operation, in the order those operations appear. A report of an empty row is an empty list. If there is no "recent" operation, return an empty list.
Function
recentlyWatched(capacity: int, operations: String[][]) → List<List<String>>Examples
Example 1
capacity = 3operations = [["watch","video1"],["watch","video2"],["watch","video3"],["recent"]]return = [["video3","video2","video1"]]The user watches video1, then video2, then video3. The most recent one comes first, so the row is ["video3","video2","video1"].
Example 2
capacity = 3operations = [["watch","a1"],["watch","b2"],["watch","c3"],["watch","a1"],["recent"],["watch","d4"],["recent"]]return = [["a1","c3","b2"],["d4","a1","c3"]]After the first three watches the row is ["c3","b2","a1"]. Watching a1 again moves it to the front instead of adding a copy, so the first report is ["a1","c3","b2"].
Watching d4 makes the row ["d4","a1","c3","b2"], which is one video too many. The least recently watched video, b2, is removed, so the second report is ["d4","a1","c3"].
Example 3
capacity = 2operations = [["recent"],["watch","x"],["watch","x"],["recent"]]return = [[],["x"]]Nothing has been watched at the first report, so it is an empty list. Watching x twice still leaves just one x in the row, so the second report is ["x"].
Constraints
1 <= capacity <= 1001 <= operations.length <= 10^4- Each operation is either
["watch", videoId]or["recent"]. - Each
videoIdhas 1 to 20 characters, and each character is a lowercase English letter or a digit.
More Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LRU CacheLotusFlare · ONSITE INTERVIEW
- LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LRU Cache OperationsHevo Data · ONSITE INTERVIEW
- LRU Cache OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- LRU Cache Operation SequenceGoogle · ONSITE INTERVIEWGoogle interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheAMD · ONSITE INTERVIEWAMD interview questions
- Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)