PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This set evaluates interval scheduling and constrained optimization for weighted, at-most-K selection, tree structure manipulation and subtree effects on height, and tree pathfinding with parent/child navigation, testing competencies in dynamic programming-like optimization, data structures (trees/binary trees), traversal, and ancestor relationships. These problems are commonly asked in Coding & Algorithms interviews because they probe algorithmic efficiency and correctness under large constraints and require both conceptual understanding of algorithmic trade-offs and practical implementation skills.

  • hard
  • Snowflake
  • Coding & Algorithms
  • Software Engineer

Solve scheduling and tree path problems

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given three separate coding problems (asked across two rounds). For each, write a function that returns the required output. ## Problem 1: Max credits with at most K non-overlapping courses You are given `n` courses. Course `i` has: - start time `s[i]` - end time `e[i]` (assume a course occupies the half-open interval `[s[i], e[i])` so a course ending at time `t` does not conflict with another starting at `t`) - credit value `c[i]` You may take **at most `K` courses**, and you may not take overlapping courses. **Output:** the maximum total credits you can earn. **Inputs:** arrays `s, e, c` and integer `K`. **Constraints (assume typical interview ranges):** `1 ≤ n ≤ 2e5`, `1 ≤ K ≤ n`, times up to `1e9`, credits up to `1e9`. --- ## Problem 2: Height of a rooted tree after deleting a node (subtree removal) You are given a **rooted** binary tree (or general rooted tree—state assumptions in your solution) and a node `x`. Operation: **remove node `x` and its entire subtree** from the tree. **Output:** the height of the remaining tree (define height as number of edges on the longest path from the root to any remaining node; height of an empty tree is `-1` or `0`—state your convention). You may be asked to answer this for one query or for multiple queries. --- ## Problem 3: Directions between two nodes in a binary tree You are given a binary tree and two node values `startValue` and `destValue`. You must output a string describing how to move from the `startValue` node to the `destValue` node using: - `L`: move to left child - `R`: move to right child - `U`: move to parent **Output:** the direction string representing a valid shortest path from start to destination.

Quick Answer: This set evaluates interval scheduling and constrained optimization for weighted, at-most-K selection, tree structure manipulation and subtree effects on height, and tree pathfinding with parent/child navigation, testing competencies in dynamic programming-like optimization, data structures (trees/binary trees), traversal, and ancestor relationships. These problems are commonly asked in Coding & Algorithms interviews because they probe algorithmic efficiency and correctness under large constraints and require both conceptual understanding of algorithmic trade-offs and practical implementation skills.

Part 1: Max credits with at most K non-overlapping courses

You are given three arrays of equal length: s, e, and c. Course i starts at time s[i], ends at time e[i], and gives c[i] credits. A course occupies the half-open interval [s[i], e[i]), so a course ending at time t does not conflict with another course starting at time t. Choose at most K non-overlapping courses so that the total earned credits are maximized. Return that maximum total.

Constraints

  • 1 <= len(s) == len(e) == len(c) <= 2 * 10^5
  • 1 <= K <= n, where n = len(s)
  • 1 <= s[i] < e[i] <= 10^9
  • 1 <= c[i] <= 10^9
  • For this interview version, assume n * K <= 2 * 10^6 so an O(nK) dynamic programming solution is acceptable.

Examples

Input: ([1, 2, 4, 6], [3, 5, 6, 7], [50, 10, 40, 70], 2)

Expected Output: 120

Explanation: Take courses [1,3) worth 50 and [6,7) worth 70 for a total of 120.

Input: ([1, 3, 3], [3, 5, 4], [10, 20, 30], 2)

Expected Output: 40

Explanation: Because intervals are half-open, [1,3) and [3,4) do not overlap. Their total is 40.

Input: ([1, 2, 3], [4, 5, 6], [5, 100, 6], 1)

Expected Output: 100

Explanation: With only one allowed course, choose the course with 100 credits.

Input: ([5], [10], [7], 1)

Expected Output: 7

Explanation: Single-course edge case.

Hints

  1. Sort courses by end time. For each course, use binary search to find the last course that finishes no later than its start time.
  2. Let dp[k][i] mean the best answer using the first i sorted courses and taking at most k of them.

Part 2: Height of a rooted binary tree after deleting a node's subtree

You are given a rooted binary tree and several independent queries. The tree is represented by: - root: the value of the root node - nodes: a list of entries [value, leftValue, rightValue] Each node value is unique. A child value of -1 means that child is missing. For each query x, remove node x and its entire subtree from the original tree, then return the height of the remaining tree. Height is defined as the number of edges on the longest path from the root to any remaining node. The height of an empty tree is -1. All queries are independent: each query starts from the original tree, not from the result of the previous query.

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= number of queries <= 10^5
  • Each node appears exactly once in nodes
  • 1 <= node value <= 10^9 and all node values are unique
  • -1 denotes a missing child
  • Each query value exists in the tree

Examples

Input: (1, [[1, 2, 3], [2, 4, 5], [3, -1, 6], [4, -1, -1], [5, -1, -1], [6, -1, -1]], [3, 2, 1])

Expected Output: [2, 2, -1]

Explanation: Removing subtree 3 leaves depth 2 through nodes 4 or 5. Removing subtree 2 leaves depth 2 through node 6. Removing the root leaves an empty tree.

Input: (1, [[1, 2, -1], [2, 3, -1], [3, -1, -1]], [2, 3])

Expected Output: [0, 1]

Explanation: This is a chain 1 -> 2 -> 3. Removing 2 leaves only the root. Removing 3 leaves a tree of height 1.

Input: (7, [[7, -1, -1]], [7])

Expected Output: [-1]

Explanation: Single-node edge case. Removing the root makes the tree empty.

Input: (10, [[10, 5, 15], [5, 2, 7], [15, 12, 20], [2, -1, -1], [7, 6, 8], [12, -1, -1], [20, -1, -1], [6, -1, -1], [8, -1, -1]], [5, 15, 7])

Expected Output: [2, 3, 2]

Explanation: Removing subtree 5 leaves the deepest node at depth 2. Removing subtree 15 leaves the path through 5 -> 7 -> 6 or 8, so height 3. Removing subtree 7 removes nodes 7, 6, and 8, leaving height 2.

Hints

  1. In a DFS preorder traversal, every subtree occupies one contiguous interval.
  2. After deleting a subtree, the answer is the maximum depth of any node outside that subtree's preorder interval.

Part 3: Directions between two nodes in a binary tree

You are given a binary tree with unique node values. The tree is represented by: - root: the value of the root node - nodes: a list of entries [value, leftValue, rightValue] A child value of -1 means that child is missing. Given startValue and destValue, return a string describing the shortest path from the start node to the destination node using: - 'L' for moving to a left child - 'R' for moving to a right child - 'U' for moving to the parent

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= node value <= 10^9 and all node values are unique
  • -1 denotes a missing child
  • startValue and destValue both exist in the tree

Examples

Input: (5, [[5, 1, 2], [1, 3, -1], [2, 6, 4], [3, -1, -1], [6, -1, -1], [4, -1, -1]], 3, 6)

Expected Output: "UURL"

Explanation: 3 -> 1 -> 5 -> 2 -> 6, so the moves are U, U, R, L.

Input: (2, [[2, 1, -1], [1, -1, -1]], 2, 1)

Expected Output: "L"

Explanation: The destination is the left child of the start node.

Input: (2, [[2, 1, 3], [1, -1, -1], [3, -1, -1]], 1, 3)

Expected Output: "UR"

Explanation: Move up from 1 to 2, then right to 3.

Input: (7, [[7, -1, -1]], 7, 7)

Expected Output: ""

Explanation: Start and destination are the same node.

Input: (8, [[8, 3, 10], [3, 1, 6], [10, -1, 14], [1, -1, -1], [6, 4, 7], [14, 13, -1], [4, -1, -1], [7, -1, -1], [13, -1, -1]], 4, 3)

Expected Output: "UU"

Explanation: 4 goes up to 6, then up again to 3.

Hints

  1. Any shortest path in a tree goes up from the start node to the lowest common ancestor, then down to the destination.
  2. If you store each node's parent and whether it is a left or right child, you can reconstruct the answer without explicitly building an LCA structure.
Last updated: Jun 8, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a JSON Parser - Snowflake (hard)
  • Solve Matrix and Array Distance Problems - Snowflake (medium)
  • Solve Array Distance and Wiki Navigation - Snowflake (medium)
  • Implement Document Predicate APIs - Snowflake (medium)
  • Find Shortest Wiki Click Path - Snowflake (medium)