FastPrepPage Referrer Reachability

Page Referrer Reachability

Amazon logoAmazon● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given a list of web traffic records. Each record is represented as a two-element string array [url, referrer]:

  • url is the page that a visitor reached.
  • referrer is the page the visitor came from. An empty string represents a direct arrival with no referrer.

Every record whose referrer is non-empty creates a directed movement from referrer to url. The reverse movement is not implied.

You are also given query = [start, target]. Return true if target is reachable from start by following zero or more recorded movements. Therefore, return true when start and target are the same page. Otherwise, return false.

Direct-arrival records do not create additional movements.

Function

canReachPage(records: String[][], query: String[]) → boolean

Examples

Example 1

records = [["/catalog","/home"],["/item/7","/catalog"],["/cart","/item/7"]]query = ["/home","/cart"]return = true

The records create the directed path /home to /catalog to /item/7 to /cart.

Example 2

records = [["/pricing","/home"],["/signup","/pricing"]]query = ["/pricing","/home"]return = false

The record creates a movement from /home to /pricing, but not the reverse movement requested by the query.

Example 3

records = [["/landing",""],["/account","/landing"]]query = ["/account","/account"]return = true

A page is reachable from itself without following an edge.

Constraints

  • 1 <= records.length <= 2 * 10^5.
  • Every row in records contains exactly two strings in the order [url, referrer].
  • Every url, start, and target is non-empty and has length at most 100.
  • Every referrer is either an empty string or a page URL of length at most 100.
  • query.length == 2.
  • Page URLs are compared exactly and are case-sensitive.
  • The records may contain repeated movements and directed cycles.

More Amazon problems

See Amazon hiring insights
public boolean canReachPage(String[][] records, String[] query) {
    // Write your code here
}
records[["/catalog","/home"],["/item/7","/catalog"],["/cart","/item/7"]]
query["/home","/cart"]
expectedtrue
Checking account…