Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
You are given a stream of n videos, each represented by a distinct number from 1 to n that you need to "upload" to a server. You need to implement a data structure that calculates the length of the longest uploaded prefix at various points in the upload process.
We consider i to be an uploaded prefix if all videos in the range 1 to i (inclusive) have been uploaded to the server. The longest uploaded prefix is the maximum value of i that satisfies this definition.
Implement the LUPrefix class:
LUPrefix(int n) Initializes the object for a stream of n videos.void upload(int video) Uploads video to the server.int longest() Returns the length of the longest uploaded prefix defined above.Example 1:
Input
["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"]
[[4], [3], [], [1], [], [2], []]
Output
[null, null, 0, null, 1, null, 3]
Explanation
LUPrefix server = new LUPrefix(4); // Initialize a stream of 4 videos.
server.upload(3); // Upload video 3.
server.longest(); // Since video 1 has not been uploaded yet, there is no prefix.
// So, we return 0.
server.upload(1); // Upload video 1.
server.longest(); // The prefix [1] is the longest uploaded prefix, so we return 1.
server.upload(2); // Upload video 2.
server.longest(); // The prefix [1,2,3] is the longest uploaded prefix, so we return 3.
Constraints:
1 <= n <= 1051 <= video <= nvideo are distinct.2 * 105 calls in total will be made to upload and longest.longest.Problem summary: You are given a stream of n videos, each represented by a distinct number from 1 to n that you need to "upload" to a server. You need to implement a data structure that calculates the length of the longest uploaded prefix at various points in the upload process. We consider i to be an uploaded prefix if all videos in the range 1 to i (inclusive) have been uploaded to the server. The longest uploaded prefix is the maximum value of i that satisfies this definition. Implement the LUPrefix class: LUPrefix(int n) Initializes the object for a stream of n videos. void upload(int video) Uploads video to the server. int longest() Returns the length of the longest uploaded prefix defined above.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Binary Search · Union-Find · Design · Segment Tree
["LUPrefix","upload","longest","upload","longest","upload","longest"] [[4],[3],[],[1],[],[2],[]]
design-an-ordered-stream)find-x-value-of-array-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2424: Longest Uploaded Prefix
class LUPrefix {
private int r;
private Set<Integer> s = new HashSet<>();
public LUPrefix(int n) {
}
public void upload(int video) {
s.add(video);
while (s.contains(r + 1)) {
++r;
}
}
public int longest() {
return r;
}
}
/**
* Your LUPrefix object will be instantiated and called as such:
* LUPrefix obj = new LUPrefix(n);
* obj.upload(video);
* int param_2 = obj.longest();
*/
// Accepted solution for LeetCode #2424: Longest Uploaded Prefix
type LUPrefix struct {
r int
s []bool
}
func Constructor(n int) LUPrefix {
return LUPrefix{0, make([]bool, n+1)}
}
func (this *LUPrefix) Upload(video int) {
this.s[video] = true
for this.r+1 < len(this.s) && this.s[this.r+1] {
this.r++
}
}
func (this *LUPrefix) Longest() int {
return this.r
}
/**
* Your LUPrefix object will be instantiated and called as such:
* obj := Constructor(n);
* obj.Upload(video);
* param_2 := obj.Longest();
*/
# Accepted solution for LeetCode #2424: Longest Uploaded Prefix
class LUPrefix:
def __init__(self, n: int):
self.r = 0
self.s = set()
def upload(self, video: int) -> None:
self.s.add(video)
while self.r + 1 in self.s:
self.r += 1
def longest(self) -> int:
return self.r
# Your LUPrefix object will be instantiated and called as such:
# obj = LUPrefix(n)
# obj.upload(video)
# param_2 = obj.longest()
// Accepted solution for LeetCode #2424: Longest Uploaded Prefix
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2424: Longest Uploaded Prefix
// class LUPrefix {
// private int r;
// private Set<Integer> s = new HashSet<>();
//
// public LUPrefix(int n) {
// }
//
// public void upload(int video) {
// s.add(video);
// while (s.contains(r + 1)) {
// ++r;
// }
// }
//
// public int longest() {
// return r;
// }
// }
//
// /**
// * Your LUPrefix object will be instantiated and called as such:
// * LUPrefix obj = new LUPrefix(n);
// * obj.upload(video);
// * int param_2 = obj.longest();
// */
// Accepted solution for LeetCode #2424: Longest Uploaded Prefix
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2424: Longest Uploaded Prefix
// class LUPrefix {
// private int r;
// private Set<Integer> s = new HashSet<>();
//
// public LUPrefix(int n) {
// }
//
// public void upload(int video) {
// s.add(video);
// while (s.contains(r + 1)) {
// ++r;
// }
// }
//
// public int longest() {
// return r;
// }
// }
//
// /**
// * Your LUPrefix object will be instantiated and called as such:
// * LUPrefix obj = new LUPrefix(n);
// * obj.upload(video);
// * int param_2 = obj.longest();
// */
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.