Skip to main content

24 docs tagged with "dfs"

View all tags

Average of Levels in Binary Tree

This question is asked by Facebook. Given a reference to the root of a binary tree, return a list containing the average value in each level of the tree.

Friend Circles

This question is asked by Facebook. You are given a two dimensional matrix, friends, that represents relationships between coworkers in an office. If friendsi = 1 then coworker i is friends with coworker j and coworker j is friends with coworker i. Similarly if friendsi = 0 then coworker i is not friends with coworker j and coworker j is not friend with coworker i. Friendships in the office are transitive (i.e., if coworker one is friends with coworker two and coworker two is friends with coworker three, coworker one is also friends with coworker three). Given the friendships in the office defined by friends, return the total number of distinct friends groups in the office.

Is Graph Bipartite

This question is asked by Facebook. Given an undirected graph, determine whether it is bipartite.

Keys and Rooms

This question is asked by Amazon. Given N distinct rooms that are locked, we want to know if you can unlock and visit every room. Each room has a list of keys in it that allows you to unlock and visit other rooms. We start with room 0 being unlocked. Return whether or not we can visit every room.

Kill Process

You are given two lists of integers and an integer representing a process id to kill. One of the lists represents a list of process ids and the other represents a list of each of the processes' corresponding (by index) parent ids. When a process is killed, all of its children should also be killed. Return a list of all the process ids that are killed as a result of killing the requested process.

Largest Pond Size

You are given a two-dimensional matrix that represents a plot of land. Within the matrix there exist two values: ones which represent land and zeroes which represent water within a pond. Given that parts of a pond can be connected both horizontally and vertically (but not diagonally), return the largest pond size.

Max Area of Island

Given a 2D array grid, where zeroes represent water and ones represent land, return the size of the largest island.

Number of Islands

Given a 2D array of integers with ones representing land and zeroes representing water, return the number of islands in the grid.

Number of Ships in Harbor

You're about to set sail off a pier and first want to count the number of ships that are already in the harbor. The harbor is deemed safe to sail in if the number of boats in the harbor is strictly less than limit. Given a 2D array that presents the harbor, where O represents water and S represents a ship, return whether or not it's safe for you to set sail.

Path Sum

Given a binary tree and a target, return whether or not there exists a root to leaf path such that all values along the path sum to the target.

Path Sum III

Given the reference to the root of a binary tree and a value k, return the number of paths in the tree such that the sum of the nodes along the path equals k.

Path with Maximum Gold

This question is asked by Amazon. Given a 2D matrix that represents a gold mine, where each cell's value represents an amount of gold, return the maximum amount of gold you can collect given the following rules:

Range Sum of BST

This question is asked by Facebook. Given the root of a binary tree and two values low and high, return the sum of all values in the tree that are within low and high (inclusive).

Subtree of Another Tree

This question is asked by Amazon. Given two trees s and t, return whether or not t is a subtree of s.

Symmetric Tree

Given a binary tree, return whether or not it forms a reflection across its center (i.e. a line drawn straight down starting from the root).

Trim Dead Nodes from Binary Tree

You are given the reference to the root of a binary tree and are asked to trim the tree of "dead" nodes. A dead node is a node whose value is listed in the provided dead array. Once the tree has been trimmed of all dead nodes, return a list containing references to the roots of all the remaining segments of the tree.

Word Search

This question is asked by Amazon. Given a 2D board that represents a word search puzzle and a string word, return whether or the given word can be formed in the puzzle by only connecting cells horizontally and vertically.