PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers

Quick Overview

This question evaluates proficiency in graph algorithms and backtracking techniques, including traversal strategies, cycle avoidance, and state management for matching a target sequence within a graph.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Determine Whether a Word Exists in a Graph

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a graph as a list of node objects. Each node contains a character and a list of neighboring nodes that can be visited next. Design and implement a function: `boolean existsPath(List<Node> nodes, String target)` Return `true` if there exists a path whose node characters, in order, exactly form `target`. The path may start from any node. During one attempted match, the same node cannot be reused. The graph may contain cycles and repeated characters. Example node shape: `Node { char value; List<Node> neighbors; }` Discuss edge cases and the time and space complexity. Possible follow-ups include handling very large graphs, pruning repeated searches, and adapting the solution if nodes may be reused.

Quick Answer: This question evaluates proficiency in graph algorithms and backtracking techniques, including traversal strategies, cycle avoidance, and state management for matching a target sequence within a graph.

You are given a directed graph where each node stores a single character and a list of neighboring nodes that can be visited next. Your task is to determine whether there exists a simple path whose characters, in order, exactly form the given target string. For this coding problem, the graph is represented as a list where node i is stored as (value, neighbors): - value: a one-character string - neighbors: a list of indices of nodes reachable from i Implement a function solution(nodes, target) that returns True if such a path exists and False otherwise. Rules: - The path may start from any node. - During one attempted match, the same node cannot be reused. - The graph may contain cycles. - Node values may repeat. - An empty target should return True.

Constraints

  • 0 <= len(nodes) <= 200
  • 0 <= len(target) <= 15
  • Each node is represented as (value, neighbors), where value is a single-character string
  • Each neighbor index is a valid index into nodes
  • The graph is directed and may contain cycles and repeated characters
  • A node cannot be reused within the same matched path

Examples

Input: ([('C', [1]), ('A', [2]), ('T', [])], 'CAT')

Expected Output: True

Explanation: Starting at node 0 gives the path 0 -> 1 -> 2, which spells C-A-T.

Input: ([('X', [1]), ('A', [2]), ('B', []), ('A', [4]), ('B', [])], 'AB')

Expected Output: True

Explanation: The target can start from any node. For example, node 1 -> node 2 spells A-B.

Input: ([('A', [1]), ('B', [2]), ('A', [0, 3]), ('C', [])], 'ABAC')

Expected Output: True

Explanation: A valid path is 0 -> 1 -> 2 -> 3, which spells A-B-A-C. The graph contains a cycle, but the path does not reuse nodes.

Input: ([('A', [0])], 'AA')

Expected Output: False

Explanation: Even though there is a self-loop, the same node cannot be reused in one attempted match.

Input: ([], '')

Expected Output: True

Explanation: The empty string is considered matched trivially.

Input: ([('A', [1]), ('B', []), ('A', [])], 'BA')

Expected Output: False

Explanation: You can start at node 1 for 'B', but it has no outgoing edge to any 'A' node, so no valid path exists.

Hints

  1. Try starting a DFS/backtracking search from every node whose character matches the first character of the target.
  2. Use a visited set or array for the current path so cycles do not cause infinite loops and the same node is not reused.
Last updated: May 12, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL 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

  • Compute Turnstile Crossing Times - Google (hard)
  • Simulate In-Place Cellular State Updates - Google (hard)
  • Solve Shortest Paths and Rental Allocation - Google (medium)
  • Solve Two Array Optimization Problems - Google (medium)
  • Find Shortest Queue with Few Calls - Google (medium)