0% found this document useful (0 votes)
50 views4 pages

Graphs Questions

Uploaded by

PRATAP GAMING
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)
50 views4 pages

Graphs Questions

Uploaded by

PRATAP GAMING
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/ 4

Graphs Questions

Question 1 : MEDIUM
Redundant Connection
You are given a graph that started as a tree with n nodes labeled from 1 to n, with one
additional edge added. The added edge has two different vertices chosen from 1 to n, and
was not an edge that already existed. The graph is represented as an array edges of length n
where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.

Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there
are multiple answers, return the answer that occurs last in the input. [Go to Qs]

Question 2 : MEDIUM
Rotting Oranges
You are given an m x n grid where each cell can have one of three values:

● 0 representing an empty cell,


● 1 representing a fresh orange, or
● 2 representing a rotten orange.
Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes
rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If
this is impossible, return -1. [Go to Qs]

Question 3 : MEDIUM
Max Area of Island
You are given an m x n binary matrix grid. An island is a group of 1's (representing land)
connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid
are surrounded by water.

The area of an island is the number of cells with a value 1 in the island.

Return the maximum area of an island in the grid. If there is no island, return 0. [Go to Qs]
Question 4 : MEDIUM
Word Ladder
A transformation sequence from word beginWord to word endWord using a dictionary
wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:

Every adjacent pair of words differs by a single letter.


Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in
wordList. sk == endWord
Given two words, beginWord and endWord, and a dictionary wordList, return the number of
words in the shortest transformation sequence from beginWord to endWord, or 0 if no such
sequence exists. [Go to Qs]

Question 5 : HARD
Redundant Connection II
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the
root) for which all other nodes are descendants of this node, plus every node has exactly
one parent, except for the root node which has no parents.

The given input is a directed graph that started as a rooted tree with n nodes (with distinct
values from 1 to n), with one additional directed edge added. The added edge has two
different vertices chosen from 1 to n, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [ui, vi]
that represents a directed edge connecting nodes ui and vi, where ui is a parent of child vi.

Return an edge that can be removed so that the resulting graph is a rooted tree of n nodes. If
there are multiple answers, return the answer that occurs last in the given 2D-array. [Go to
Qs]

Question 6 : HARD
Couples Holding Hands
There are n couples sitting in 2n seats arranged in a row and want to hold hands.

The people and seats are represented by an integer array row where row[i] is the ID of the
person sitting in the ith seat. The couples are numbered in order, the first couple being (0,
1), the second couple being (2, 3), and so on with the last couple being (2n - 2, 2n - 1).

Return the minimum number of swaps so that every couple is sitting side by side. A swap
consists of choosing any two people, then they stand up and switch seats. [Go to Qs]
Question 7 : HARD
Course Schedule III
There are n different online courses numbered from 1 to n. You are given an array courses
where courses[i] = [durationi, lastDayi] indicate that the ith course should be taken
continuously for durationi days and must be finished before or on lastDayi.

You will start on the 1st day and you cannot take two or more courses simultaneously.

Return the maximum number of courses that you can take. [Go to Qs]

Question 8 : HARD
Alien Dictionary
There is a new alien language which uses the latin alphabet. However, the order among
letters are unknown to you. You receive a list of non-empty words from the dictionary, where
words are sorted lexicographically by the rules of this new language. Derive the order of
letters in this language. [Go to Qs]

Input:
[
"wrt",
"wrf",
"er",
"ett"
,
"rftt"
]

Output: "wertf"

Question 9 : MEDIUM
Number of Closed Islands
Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally
connected group of 0s and a closed island is an island totally (all left, top, right, bottom)
surrounded by 1s.

Return the number of closed islands [Go to Qs]


Question 10 : MEDIUM
Shortest Path with Alternating Colors
You are given an integer n, the number of nodes in a directed graph where the nodes are
labeled from 0 to n - 1. Each edge is red or blue in this graph, and there could be self-edges
and parallel edges.

You are given two arrays redEdges and blueEdges where:

redEdges[i] = [ai, bi] indicates that there is a directed red edge from node ai to node bi in the
graph, and
blueEdges[j] = [uj, vj] indicates that there is a directed blue edge from node uj to node vj in
the graph.
Return an array answer of length n, where each answer[x] is the length of the shortest path
from node 0 to node x such that the edge colors alternate along the path, or -1 if such a path
does not exist. [Go to Qs]

https://telegram.me/+QGUyTripaP4zNTcx

https://telegram.me/+nEKeBr_yhXtmY2Yx

Say Hi to TG for Further Updates:


https://t.me/RepublicDayBot

#TGS

You might also like