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
Graph Traversal Coding Problem
00:00
5 left

Graph Traversal Coding Problem

HardPython

Problem

TikTok Trust and Safety services route security events through directed processing nodes. Each connection has a positive latency, and an incident may use at most one emergency firewall bypass that reduces the latency of one connection to floor(latency / 2).

Given the graph, source, and destination, return the minimum possible total latency. The bypass may be unused, and each connection can be traversed at most as part of the selected route. Return -1 if the destination is unreachable.

Formal Specification

Implement shortest_route_with_bypass(n, edges, source, destination).

  • n is an integer number of nodes labeled 0 through n - 1.
  • edges is a list of directed triples [u, v, latency].
  • source and destination are integer node labels.
  • Return an integer representing the minimum latency, or -1 when no route exists.

Treat each node as having two states: the bypass is unused or already used. A route cannot use the bypass more than once.

Constraints

  • 1 <= n <= 10^5
  • 0 <= len(edges) <= 2 * 10^5
  • 0 <= u, v < n and u != v
  • 1 <= latency <= 10^9
  • Parallel directed edges may exist.
  • All latencies are positive.

Function Signature

def shortest_route_with_bypass(n, edges, source, destination):
Interviewer

Your question is Graph Traversal Coding Problem. 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.