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 stringsSystem Design Questions - Reddit These are the most commonly asked system design questions from Reddit interviews.
Input: Number(s)n 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.Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free