Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Longest Path in a DAG
00:00
5 left

Longest Path in a DAG

HardPython

Problem

RAMP Consulting Group models dependency pipelines as a directed acyclic graph, where an edge u -> v means pipeline step u must complete before step v. Given the number of steps and dependency edges, return one longest pipeline as an ordered list of node IDs.

The path length is measured by the number of nodes. If multiple longest paths exist, return the lexicographically smallest path, meaning the path with the smallest differing node ID. Isolated nodes are valid paths of length one. The input is guaranteed to represent a DAG.

Formal Specification

Implement longest_pipeline_path(n, edges).

  • n is an integer representing nodes 0 through n - 1.
  • edges is a list of two-element lists [u, v], representing a directed edge from u to v.
  • Return a list of node IDs representing the selected longest path.
  • Return [] when n == 0.

Constraints

  • 0 <= n <= 100000
  • 0 <= len(edges) <= 200000
  • 0 <= u, v < n
  • u != v
  • The graph is guaranteed to be acyclic
  • Duplicate edges are absent

Function Signature

def longest_pipeline_path(n, edges):
Interviewer

Your question is Longest Path in a DAG. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.