KatInterview

Web Crawling Discovery and Bracket Syntax Check

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphsbfsgraph-traversalstack

Last updated

Perform a depth-bounded graph traversal over web pages based on domain filtering, and validate balanced brackets using a stack.

You are tasked with implementing a unified routine that processes two distinct sub-problems: traversing a simulated web graph and verifying a sequence of bracket characters.

Part 1: Graph Traversal (Web Crawler)

Given a directory of web pages pages, a list of starting seed URLs start_urls, a maximum crawling depth max_depth, and an allowed set of host domains domain_filter:

  1. A URL is permitted if its parsed hostname (e.g., urlparse(url).hostname) belongs to the set domain_filter.
  2. Discovery starts at depth 0 with all valid start_urls.
  3. Breadth-First Search (BFS) is used to explore outgoing links. An outgoing link found on a page at depth d is reached at depth d + 1.
  4. Outgoing links are explored only if the current page depth d < max_depth. If depth >= max_depth, do not inspect its outgoing links.
  5. Record all uniquely discovered valid URLs.

pages is represented as a dictionary mapping a page URL to a tuple/list: [page_title, outgoing_links_list]. If a page URL is missing from pages or has no outgoing links entry, treat its outgoing links list as empty.

Part 2: Bracket Syntax Check

Given a string brackets containing bracket characters ((, ), [, ], {, }):

  1. Validate whether the brackets match in a balanced, LIFO order.
  2. If any invalid character is encountered, or a closer fails to match the most recent unmatched opener, the error occurs at that character's 0-based index.
  3. If all closers match correctly but there are unclosed opening brackets remaining at the end, the error index is defined as len(brackets).
  4. If the bracket sequence is entirely valid and balanced, there is no error index (represented as None or null in Python/JSON output).

Return a dictionary with two keys:

  • 'discovered_urls': A lexicographically sorted list of all unique discovered valid URLs.
  • 'bracket_error_index': The integer index of the first bracket error, or None if valid.

Constraints

  • 0 <= len(start_urls) <= 10^4
  • 0 <= max_depth <= 100
  • domain_filter contains valid domain names (e.g., "example.com").
  • brackets contains only bracket characters standard to matching problems.
  • len(brackets) <= 10^5

Examples

Example 1

Input: crawl_and_validate({'https://snowflake.com/': ['home', ['https://snowflake.com/about']], 'https://snowflake.com/about': ['about', ['https://snowflake.com/jobs', 'https://other.com/']], 'https://snowflake.com/jobs': ['jobs', []]}, ['https://snowflake.com/'], 2, ['snowflake.com'], '([]{})')

Output: {'discovered_urls': ['https://snowflake.com/', 'https://snowflake.com/about', 'https://snowflake.com/jobs'], 'bracket_error_index': None}

Example 2

Input: crawl_and_validate({'https://a.com/': ['home', ['https://a.com/x', 'https://b.com/y']], 'https://a.com/x': ['x', ['https://a.com/z']], 'https://a.com/z': ['z', ['https://a.com/']]}, ['https://a.com/'], 2, ['a.com'], '([)]')

Output: {'discovered_urls': ['https://a.com/', 'https://a.com/x', 'https://a.com/z'], 'bracket_error_index': 2}

Example 3

Input: crawl_and_validate({'https://a.com/': ['home', ['https://a.com/x']], 'https://a.com/x': ['x', ['https://a.com/']]}, ['https://a.com/'], 0, ['a.com'], '(((')

Output: {'discovered_urls': ['https://a.com/'], 'bracket_error_index': 3}

Hints

Hint 1

For the crawler, use a queue for standard BFS traversal tracking `(url, depth)` and a `set` to track visited/discovered URLs to prevent duplicate visits.

Hint 2

Only expand outgoing links from a page if its current depth is strictly less than `max_depth`.

Hint 3

For bracket validation, push opening brackets onto a stack. When encountering a closing bracket, check if the stack top matches. If any character is not a standard bracket or mismatches, report the current index. If leftover openers remain after iterating, return the string's total length.