Java · Coding problem
Recently Viewed Items
A free coding problem from the Java 21: Sequenced Collections and the Rest of 18-21 skill. Read it here, then solve it in the app in Java.
- Easy
- Coding problem
- Free
- Java
The problem
An online store in Portland shows each shopper the items they viewed recently. Write RecentlyViewed:
view(item): the item becomes the most recent. Viewing an item that's already in the list moves it to the front (no duplicates). Keep at mostcapacityitems, dropping the least recent.recent(): the items, most recent first, as a list the caller can't use to change this object (later views must not change a list already handed out, and changing that list must not change this object).oldest(): the least recently viewed item, ornoneif nothing has been viewed yet.oldestFirst(): the items, least recent first.
Java 21's sequenced collections make each of these a single call on the right collection.
Example
Input: 3
view shoes
view hat
view scarf
view shoes
recent
oldest
oldestFirst
Output: recent: [shoes, scarf, hat]
oldest: hat
oldest first: [hat, scarf, shoes]
Constraints
1 <= capacity <= 50- up to 100 commands
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
Solve it Recently Viewed Items free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime