You're managing a video streaming platform where videos must be uploaded in a specific sequence. Imagine you have n videos numbered from 1 to n, but they arrive at your server in random order due to network conditions.

Your task is to build a smart system that tracks the longest consecutive prefix of videos that have been successfully uploaded starting from video 1. For example, if videos 1, 2, and 3 are uploaded (regardless of order), your longest prefix is 3. But if videos 1, 3, and 5 are uploaded, your longest prefix is only 1 (since video 2 is missing).

Design the LUPrefix class with these operations:

  • LUPrefix(int n) - Initialize for a stream of n videos
  • upload(int video) - Mark video as uploaded
  • longest() - Return length of longest uploaded prefix

This problem tests your ability to efficiently track consecutive sequences and optimize for frequent queries.

Input & Output

example_1.py โ€” Basic Upload Sequence
$ Input: LUPrefix(4), upload(3), longest(), upload(1), longest(), upload(2), longest()
โ€บ Output: [null, null, 0, null, 1, null, 3]
๐Ÿ’ก Note: After uploading video 3, longest prefix is 0 (video 1 missing). After uploading video 1, prefix becomes 1. After uploading video 2, videos 1-2-3 are consecutive, so prefix becomes 3.
example_2.py โ€” Sequential Uploads
$ Input: LUPrefix(3), upload(1), longest(), upload(2), longest(), upload(3), longest()
โ€บ Output: [null, null, 1, null, 2, null, 3]
๐Ÿ’ก Note: Videos uploaded in order 1,2,3. Each upload extends the longest prefix by 1, resulting in prefix lengths 1, 2, and 3 respectively.
example_3.py โ€” Reverse Order Uploads
$ Input: LUPrefix(2), upload(2), longest(), upload(1), longest()
โ€บ Output: [null, null, 0, null, 2]
๐Ÿ’ก Note: Upload video 2 first - prefix remains 0 since video 1 is missing. Upload video 1 - now videos 1 and 2 are both present, so prefix becomes 2.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค video โ‰ค n
  • All values of video are distinct
  • At most 2 ร— 105 calls will be made to upload and longest
  • At least one call will be made to longest

Visualization

Tap to expand
๐ŸŽต Playlist Management: Consecutive Track AvailabilityStep 1: Track 3 uploadedAvailable: [โŒ Track 1] [โŒ Track 2] [โœ… Track 3] [โŒ Track 4]Playable Prefix: 0 tracksStep 2: Track 1 uploadedAvailable: [โœ… Track 1] [โŒ Track 2] [โœ… Track 3] [โŒ Track 4]Playable Prefix: 1 track (Track 1 only)Step 3: Track 2 uploaded - Prefix extends!Available: [โœ… Track 1] [โœ… Track 2] [โœ… Track 3] [โŒ Track 4]Playable Prefix: 3 tracks (Tracks 1โ†’2โ†’3)๐Ÿ’ก Algorithm Insight:โ€ข Keep a pointer to the current longest consecutive prefixโ€ข When uploading: mark as available, then try extending the prefixโ€ข Result: O(1) queries, amortized O(1) uploads
Understanding the Visualization
1
Track 3 arrives
Can't play anything yet - track 1 is missing, so playable prefix is 0
2
Track 1 arrives
Now we can play track 1, but track 2 is missing, so prefix length is 1
3
Track 2 arrives
Perfect! Now tracks 1, 2, and 3 are consecutive, so we can play up to track 3
Key Takeaway
๐ŸŽฏ Key Insight: Instead of scanning from the beginning each time, maintain the current longest prefix and only extend it when the next consecutive video becomes available!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen