Find top-k rated nodes via traversal

Quick Overview

This question evaluates understanding of graph traversal combined with streaming top-k selection, testing competencies in graph algorithms, data structure choice (e.g., visited sets and bounded heaps), tie-breaking, and reachable-node enumeration within the Coding & Algorithms domain.

Find top-k rated nodes via traversal

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a finite graph (directed or undirected) with n nodes and m edges. Each node u has an integer rating r[u]. Given a starting node s and an integer k, return the top k distinct nodes (excluding s only if specified) reachable from s by traversing edges. Break ties by smaller node ID. The graph may contain cycles. Design and implement an algorithm that: (a) visits each reachable node at most once (e.g., via BFS/DFS), (b) maintains the current top k efficiently during traversal, and (c) outputs nodes in descending rating order. Analyze time and space complexity, justify your data-structure choices (e.g., visited set and a size-k heap), and discuss how your solution changes if k is close to the number of reachable nodes, if ratings can change during traversal, or if the graph is too large to fit in memory.

Quick Answer: This question evaluates understanding of graph traversal combined with streaming top-k selection, testing competencies in graph algorithms, data structure choice (e.g., visited sets and bounded heaps), tie-breaking, and reachable-node enumeration within the Coding & Algorithms domain.

|Home/Coding & Algorithms/TikTok
TikTok logo
TikTok
Sep 6, 2025, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
7
0

You are given a finite graph (directed or undirected) with n nodes and m edges. Each node u has an integer rating r[u]. Given a starting node s and an integer k, return the top k distinct nodes (excluding s only if specified) reachable from s by traversing edges. Break ties by smaller node ID. The graph may contain cycles. Design and implement an algorithm that: (a) visits each reachable node at most once (e.g., via BFS/DFS), (b) maintains the current top k efficiently during traversal, and (c) outputs nodes in descending rating order. Analyze time and space complexity, justify your data-structure choices (e.g., visited set and a size-k heap), and discuss how your solution changes if k is close to the number of reachable nodes, if ratings can change during traversal, or if the graph is too large to fit in memory.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...