0% found this document useful (0 votes)
13 views18 pages

Model Questions

The document contains a series of model questions related to graph and tree data structures, including problems like printing adjacency lists, finding shortest paths, detecting cycles, and calculating the number of islands. Each problem is accompanied by example inputs and outputs to illustrate the expected results. The document serves as a resource for practicing algorithmic problem-solving in computer science.

Uploaded by

Nandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views18 pages

Model Questions

The document contains a series of model questions related to graph and tree data structures, including problems like printing adjacency lists, finding shortest paths, detecting cycles, and calculating the number of islands. Each problem is accompanied by example inputs and outputs to illustrate the expected results. The document serves as a resource for practicing algorithmic problem-solving in computer science.

Uploaded by

Nandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

WAFFOR RETAIL SOLUTION P LTD


​ MODEL QUESTIONS

Graph Questions

1.​ Print Adjacency List for a Directed Graph

Problem: Given the adjacency list and number of vertices and edges of a graph,
the task is to represent the adjacency list for a directed graph.

Examples:

Input: V = 3, edges[][]= {{0, 1}, {1, 2} {2, 0}}​

Output: 0 -> 1​
1 -> 2​
2 -> 0
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

2.​ Shortest Path in Binary Matrix


Given an n x n binary matrix grid, return the length of the shortest clear
path in the matrix. If there is no clear path, return -1.
A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0))
to the bottom-right cell (i.e., (n - 1, n - 1)) such that:

All the visited cells of the path are 0.


All the adjacent cells of the path are 8-directionally connected.
The length of a clear path is the number of visited cells of this path.

Example 1:


​ Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
Output: 4

​ Example 2:
Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
Output: -1
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

3.​ Detect A Cycle in Directed Graph using DFS


Problem Statement: Given is a 2D adjacency list representation of a directed
graph. Check whether the graph has cycles or not.

Example:

Input:
Graph= {
0 -> 1
1 -> 2, 5
2 -> 3
3 -> 4
4 -> 0, 1
5 -> null
}

Output: Cycle detected


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

4.​ Cheapest Flights Within K Stops


There are n cities and m edges connected by some number of flights. You are
given an array of flights where flights[i] = [ from (i), to (i), price (i)] indicates that
there is a flight from city from (i) to city to (i) with cost price. You have also given
three integers src, dst, and k, and return the cheapest price from src to dst with
at most k stops. If there is no such route, return -1.

Example :

Input:
n=4
flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]]
src = 0
dst = 3
k=1

Output:
700
Explanation:
The optimal path with at most 1 stops from city 0 to 3 is marked in red and has
cost 100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it
uses 2 stops.
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

5.​ Rotten Oranges : Min time to rot all oranges


​ Problem Statement: You will be given an m x n grid, where each cell has
the following values :

2 - represents a rotten orange


1 - represents a Fresh orange
0 - represents an Empty Cell
Every minute, if a Fresh Orange is adjacent to a Rotten Orange in 4-direction (
upward, downwards, right, and left ) it becomes Rotten.

Return the minimum number of minutes required such that none of the cells has
a Fresh Orange. If it's not possible, return -1.

Example:
Input: grid - [ [2,1,1] , [1,1,0] , [0,1,1] ]
Output: 4
​ Explanation:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

6.​ Find the Number of Islands

Given a binary 2D matrix, find the number of islands. A group of connected 1s


forms an island. For example, the below matrix contains 4 islands.
Examples:
Input: M[][] = {{‘1’, ‘1’, ‘0’, ‘0’, ‘0’},
{‘0’, ‘1’, ‘0’, ‘0’, ‘1’},
{‘1’, ‘0’, ‘0’, ‘1’, ‘1’},
{‘0’, ‘0’, ‘0’, ‘0’, ‘0’},
{‘1’, ‘0’, ‘1’, ‘1’, ‘0’}}
Output: 4
Explanation: The image below shows all the 4 islands in the graph
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

7.​ Count the largest island size



Given a binary 2D matrix, find the size of the largest island. A group of
connected 1s forms an island. For example, in the below matrix the largest
island size is 5.

Examples:
Input: M[][] = {{‘1’, ‘1’, ‘0’, ‘0’, ‘0’},
{‘0’, ‘1’, ‘0’, ‘0’, ‘1’},
{‘1’, ‘1’, ‘0’, ‘1’, ‘1’},
{‘0’, ‘0’, ‘0’, ‘0’, ‘0’},
{‘1’, ‘0’, ‘1’, ‘1’, ‘0’}}
Output: 5
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

8.​ All Paths From Source to Target

Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all
possible paths from node 0 to node n - 1 and return them in any order.

The graph is given as follows: graph[i] is a list of all nodes you can visit from
node i (i.e., there is a directed edge from node i to node graph[i][j]).

Example :


Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

9.​ Find a Mother Vertex in a Graph


The problem states that we are given a graph G, and there are E edges and V
vertices. Mother Vertex is defined as that vertex v, from which all other vertices
of the graph are reachable. There can be zero, one, or many mother vertex in
the given graph, we need to find all the mother vertices.

Example:
Input :

Output: 0 10 40
Explanation: You can check from the given graph, that all nodes are reachable
from nodes 0, 10, and 40.
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

10.​Course Schedule

There are a total of numCourses courses you have to take, labeled from 0 to
numCourses - 1. You are given an array of prerequisites where prerequisites[i] =
[ai, bi] indicates that you must take course bi first if you want to take course ai.

For example, the pair [0, 1], indicates that to take course 0 you have to first take
course 1.
Return true if you can finish all courses. Otherwise, return false.

Example 1:

Input: numCourses = 2, prerequisites = [[1,0]]


Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.

Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you
should also have finished course 1. So it is impossible.
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

Tree

1.​ Symmetric Tree


Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric
around its center)

Input: root = [1,2,2,3,4,4,3]


Output: true

2.​ Convert Sorted List to Binary Search Tree


Given the head of a singly linked list where elements are sorted in ascending order,
convert it to a height-balanced binary search tree

Input: head = [-10,-3,0,5,9]


Output: [0,-3,9,-10,null,5]
Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown
height balanced BST.
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

3.​ Path Sum


Given the root of a binary tree and an integer targetSum, return true if the tree has a
root-to-leaf path such that adding up all the values along the path equals targetSum.

A leaf is a node with no children.

Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22


Output: true
Explanation: The root-to-leaf path with the target sum is shown.

4.​ Kth Smallest Element in a BST


Given the root of a binary search tree, and an integer k, return the kth smallest value
(1-indexed) of all the values of the nodes in the tree.

Input: root = [5,3,6,2,4,null,null,1], k = 3


Output: 3
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

5.​ Sum Root to Leaf Numbers


You are given the root of a binary tree containing digits from 0 to 9 only.

Each root-to-leaf path in the tree represents a number.

For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
Return the total sum of all root-to-leaf numbers.

A leaf node is a node with no children.

Input: root = [1,2,3]


Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

6.​ Find Bottom Left Tree Value


Given the root of a binary tree, return the leftmost value in the last row of the tree.

Input: root = [1,2,3,4,null,5,6,null,null,7]


Output: 7
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
7.​ Add One Row to Tree
Given the root of a binary tree and two integers val and depth, add a row of nodes with
value val at the given depth depth.

Note that the root node is at depth 1.

The adding rule is:

1.​ Given the integer depth, for each not null tree node cur at the depth depth - 1,
create two tree nodes with value val as cur's left subtree root and right subtree
root.
2.​ cur's original left subtree should be the left subtree of the new left subtree root.
3.​ cur's original right subtree should be the right subtree of the new right subtree
root.
4.​ If depth == 1 that means there is no depth depth - 1 at all, then create a tree
node with value val as the new root of the whole original tree, and the original
tree is the new root's left subtree.

Input: root = [4,2,null,3,1], val = 1, depth = 3


Output: [4,2,null,1,1,3,null,null,1]
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

8.​ Find Duplicate Subtrees


Given the root of a binary tree, return all duplicate subtrees.

For each kind of duplicate subtrees, you only need to return the root node of any one of
them.

Two trees are duplicate if they have the same structure with the same node values.

Input: root = [1,2,3,4,null,2,4,null,null,4]


Output: [[2,4],[4]]
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

9.​ Maximum Width of Binary Tree


Given the root of a binary tree, return the maximum width of the given tree.

The maximum width of a tree is the maximum width among all levels.

The width of one level is defined as the length between the end-nodes (the leftmost and
rightmost non-null nodes), where the null nodes between the end-nodes that would be
present in a complete binary tree extending down to that level are also counted into the
length calculation.

Input: root = [1,3,2,5,3,null,9]


Output: 4
Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

10.​Check Completeness of a Binary Tree


Given the root of a binary tree, determine if it is a complete binary tree.

In a complete binary tree, every level, except possibly the last, is completely filled, and
all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes
inclusive at the last level h.

Input: root = [1,2,3,4,5,6]


Output: true
Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}),
and all nodes in the last level ({4, 5, 6}) are as far left as possible.

You might also like