PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency with graph algorithms, shortest-path and reachability concepts when nodes are removed or blocked, and the ability to analyze time and space complexity across directed and undirected graphs.

  • Medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Compute shortest paths with blocked nodes

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a graph with nodes and edges and a designated source node s, compute the shortest distance from s to every other node. Some nodes are inaccessible (blocked) and cannot be traversed or reached; treat them as removed from the graph. Return a distance for each node where unreachable nodes (including blocked ones) are -1. Describe your algorithm, data structures, and time/space complexity, and note any differences for directed vs. undirected graphs.

Quick Answer: This question evaluates proficiency with graph algorithms, shortest-path and reachability concepts when nodes are removed or blocked, and the ability to analyze time and space complexity across directed and undirected graphs.

Given n nodes labeled 0..n-1, edges, a source, and blocked nodes, return shortest unweighted distances from the source. Blocked and unreachable nodes must be -1. Set directed=True to treat edges as directed.

Constraints

  • Nodes are labeled 0 through n-1
  • Edges are unweighted

Examples

Input: (5, [[0, 1], [1, 2], [0, 3], [3, 4]], 0, [3], False)

Expected Output: [0, 1, 2, -1, -1]

Input: (4, [[0, 1], [1, 2], [2, 3]], 0, [], True)

Expected Output: [0, 1, 2, 3]

Input: (3, [[0, 1]], 2, [2], False)

Expected Output: [-1, -1, -1]

Hints

  1. Treat blocked nodes as removed before BFS.
  2. For directed graphs, only add the given edge direction.
Last updated: Jun 27, 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
  • AI Coding 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

  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
  • Most Active Users in a Live Communication Stream - Google (medium)
  • Boolean Expression Tree with Leaf Flips - Google (medium)
  • Streaming Points: Remove Any Pair Within a Distance - Google (medium)
  • Solve Rooms and Top-K Streams - Google (medium)