Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
DFS vs BFS Choice
00:00
5 left

DFS vs BFS Choice

MediumPython

Problem

Explain why you might choose DFS over BFS for a graph or tree problem. Implement dfs_path(graph, start, target) using iterative DFS, returning the first path discovered in the listed neighbor order, or [] if no path exists. The graph is a directed adjacency-list dictionary whose node identifiers are strings; return a list of node identifiers.

Constraints

  • 1 <= number of graph nodes <= 10^4
  • Each node identifier is a string
  • The graph is directed and represented by an adjacency-list dictionary
  • Neighbor lists contain valid string node identifiers
  • Return the path following listed neighbor order when multiple DFS paths are possible

Function Signature

def dfs_path(graph, start, target):
Interviewer

Your question is DFS vs BFS Choice. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.