Katkat

Solve Array Distance and Wiki Navigation

SnowflakeSoftware Engineer · Online Assessment

Last updated

This question evaluates array-processing and graph-algorithm skills, specifically minimum-distance computation between marked elements in a one-dimensional array and shortest-path navigation in a directed link graph, plus edge-case reasoning and test-case design.

You are given two independent coding tasks. For each task, clarify edge cases, implement the function, and write meaningful test cases.

Task 1: Minimum Distance Between a Person and a Cake

You are given a one-dimensional array arr where each element is one of:

  • 0: empty space
  • 1: a person
  • 2: a cake

Return the minimum absolute distance between any person and any cake. The distance between positions i and j is abs(i - j).

If the array does not contain at least one person and at least one cake, return -1.

Example:

Input: [0, 1, 0, 0, 2, 0, 1]
Output: 2
Explanation: The closest person-cake pair is at indices 4 and 6.

Task 2: Minimum Clicks Between Wiki Pages

A wiki page can contain links to other wiki pages. Given a startPage and a targetPage, return the minimum number of clicks needed to navigate from startPage to targetPage by following links.

You should also implement a simulator for retrieving all outgoing links from a wiki page. For example, you may model the wiki as an in-memory directed graph and implement:

getLinks(page) -> list of pages directly linked from page

Requirements:

  • Each click follows exactly one outgoing link.
  • If startPage == targetPage, return 0.
  • If the target page is unreachable, return -1.
  • The graph may contain cycles, so avoid revisiting pages.
  • Write tests covering reachable pages, unreachable pages, cycles, and the same start and target page.