Reddit software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
id and...Input: Listrelationships, where each element is a list of strings. The first string represents a...Input: Array of stringsn comments numbered 0 through n - 1. The array parent specifies each comment's parent; parent[i] = -1 means comment i is a top-level comment. Top-level comments and each comment's immediate children are ordered by increasing comment ID. The complete display order is depth-first preorder: display a comment, then recursively display its children in order.Initially, every comment is expanded. Process the following operations in order:("collapse", id): mark the comment collapsed. All of its strict descendants become hidden. The comment itself remains visible unless an ancestor is collapsed.("expand", id): clear that comment's collapsed state. Its descendants become visible only if they have no other collapsed ancestor.("select", k): return the ID of the k-th visible comment, using a 1-based position in display order. Return -1 if fewer than k comments are visible.select operation. Your implementation must use O(n + q log n) time and O(n) auxiliary space, excluding the input and returned results, where q is the number of operations.Function or class signaturevisible_comments(parent: list[int], operations: list[tuple[str, int]]) -> list[int]{
"parent": [-1, 0, 0, 1, 1, 2, -1],
"operations": [["select", 4], ["collapse", 1], ["select", 4], ["collapse", 0], ["select", 2], ["expand", 1], ["select", 3], ["expand", 0], ["select", 4]]
}[4, 5, 6, -1, 4]
[0, 1, 3, 4, 2, 5, 6]. Collapsing 1 leaves [0, 1, 2, 5, 6]. Collapsing 0 leaves [0, 6]. Expanding the hidden comment 1 does not expose anything while 0 remains collapsed, so position 3 does not exist. Expanding 0 then restores the complete display order because 1 is already expanded.Example 2:{
"parent": [-1, 0, 1, 1],
"operations": [["collapse", 1], ["collapse", 0], ["expand", 0], ["select", 3], ["expand", 1], ["select", 3], ["collapse", 2], ["select", 4]]
}[-1, 2, 3]
1, collapsing 0, and expanding 0, only [0, 1] are visible: comment 1 retains its collapsed state. Expanding 1 restores [0, 1, 2, 3], whose third comment is 2. Collapsing leaf 2 changes no visibility, so the fourth comment remains 3.Constraints:1 <= n <= 200000.0 <= q <= 200000.parent[i] is -1 or an integer in [0, n - 1]; the array describes a valid acyclic forest.collapse, expand, or select.collapse and expand, 0 <= id < n; for select, 1 <= k <= 200000.n; traversal must not depend on a language recursion limit proportional to n.s, return the length of the longest substring without repeating characters. A substring is defined as a contiguous sequence of characters in s. You must implement an efficient solution to handle potentially large strings common in user-generated content.def length_of_longest_substring(s: str) -> int: "abcabcbb" 3 "abc", with the length of 3."bbbbb" 1 "b", with the length of 1.s consists of English letters, digits, symbols and spaces.RateLimiter class that supports multiple APIs and handles different request quotas per user efficiently. Each user has a unique ID and can have different rate limits on different APIs. Your implementation should allow checking if a user can make a request to an API and register the request, updating the rate limit accordingly.class RateLimiter def can_request(self, user_id: str, api_name: str) -> bool: def register_request(self, user_id: str, api_name: str) -> None:rate_limiter = RateLimiter() rate_limiter.register_request('user1', 'api1') rate_limiter.can_request('user1', 'api1') True rate_limiter.register_request('user1', 'api1') rate_limiter.register_request('user1', 'api1') rate_limiter.can_request('user1', 'api1') False RateLimiter class.PriorityQueue class that supports adding threads with a given priority and returning the thread with the highest priority. The class should implement the following operations: add(thread: str, priority: int) -> None: Inserts a new thread with the specified priority into the priority queue. poll() -> str: Removes and returns the thread with the highest priority. If two threads have the same priority, return the one that was added first. peek() -> str: Returns the thread with the highest priority without removing it from the queue. class PriorityQueue: def add(self, thread: str, priority: int) -> None: def poll(self) -> str: def peek(self) -> str: Example 1: pq = PriorityQueue() pq.add("Post A", 2) pq.add("Post B", 3) pq.poll() "Post B" pq = PriorityQueue() pq.add("Post X", 1) pq.add("Post Y", 1) pq.poll() "Post X" longest_path(graph: Dict[int, List[int]], start: int) -> int where graph is a dictionary where each key represents a node and the value is the list of nodes it points to. def longest_path(graph: Dict[int, List[int]], start: int) -> int:graph = {1: [2, 3], 2: [4], 3: [], 4: []}, start = 13graph = {1: [2], 2: [3], 3: [4], 4: []}, start = 14Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free