Collection of coding questions appearing in online assessment of Swiggy during campus placements at IIT/NITs, and other top engineering colleges in India.
- Activate Fountain [IIT-BHU'22]
- Perfect Substring [IIT-BHU'22]
- Balanced Tree [IIT-BHU'22]
Fountains are installed at every position along a one-dimensional garden of length
For position
For position
For position
For the entire length of this garden to be covered, only the fountain at position
Complete the function fountainActivation
.
fountainActivation
has the following parameter:
-
$int \ \ locations[n]$ : the fountain locations
-
$int$ : the minimum number of fountains that must be activated
$1 \leq n \leq 10^5$ -
$0 \leq locations[i] \leq min(n, \ 100)$ (where$1 \leq i \leq 10^5$ )
A string comprised of digits from
The
$s[0:1] = 11$ $s[0:5] = 110202$ $s[1:6] = 102021$ $s[2:5] = 0202$ $s[7:8] = 22$ $s[8:9] = 22$
Complete the function perfectSubstring
.
perfectSubstring
has the following parameters:
-
$str \ \ s$ : a string where the value of each element$s[i]$ described by the character at position$i$ (where $0 \leq i \lt n) -
$int \ \ k$ : an integer that denotes the required frequency of the substring
-
$int$ : an integer that represents the number of perfect substrings in the given string
$1 \leq sizeof (s) \leq 10^5$ -
$0 \leq s[i] \leq 9$ (where$0 \leq i \lt n$ ) $1 \leq k \leq 10^5$
Given a rooted tree consisting of
We define a
The goal is to modify this tree such that it becomes a
Consider the following tree
The optimal way to balance the above tree is to add
Complete the function findMinStones
.
findMinStones
has the following parameters:
-
$tree \textunderscore nodes$ : integer denoting the total number of nodes in the tree -
$int \ \ tree \textunderscore from [tree \textunderscore nodes - 1]$ : integer array denoting the nodes which share an edge with nodes of$tree \textunderscore to$ array -
$int \ \ tree \textunderscore to [tree \textunderscore nodes - 1]$ : integer array denoting the nodes which share an edge with nodes of$tree \textunderscore from$ array -
$int \ \ stones[tree \textunderscore nodes]$ : integer array denoting the number of stones on each node
- The function must return an integer value, the minimum number of extra stones required to balance the tree.
$1 \leq n \leq 2 \times 10^5$ $0 \leq stones[i] \leq 10^9$
Sample Input | Sample Output |
5 4
1 2
2 3
2 4
3 5
5
1 5 7 8 3 |
10 |
Following are the optimal changes that will make the given tree balanced using minimum stones:
- Add
$2$ stones to node$'2'$ - Add
$5$ stones to node$'1'$ - Add
$3$ stones to node$'5'$
After performing the above