Practice DSA Problems ET
Practice DSA Problems ET
(by bharat)
Given a Directed Graph with V vertices (Numbered from 0 to V-1) and E edges, check
whether it contains any cycle or not.
Input:
First line contains two integers V and E which represent the number of vertex
and number of edges in directed graph respectively.
Next all lines contains two integers v1 and v2which represent the two vertex
and edge between it.
Output:
Print true or false (graph has cycle or not)
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's
(water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands
horizontally or vertically. You may assume all four edges of the grid are all surrounded
by water.
Input:
First line contains two integers m and n which represent the number of rows
and number of columns in 2D binary grid respectively.
There is an undirected graph with n nodes, where each node is numbered between 0
and n - 1. You are given a 2D array graph, where graph[u] is an array of nodes that
node u is adjacent to. More formally, for each v in graph[u], there is an undirected edge
between node u and node v. The graph has the following properties:
A graph is bipartite if the nodes can be partitioned into two independent sets A and B
such that every edge in the graph connects a node in set A and a node in set B.
Input:
First line contains two integers V and E which represent the number of vertex
and number of edges in directed graph respectively.
Next all lines contains two integers v1 and v2 which represent the two vertex
and edge between it.
Output:
print true or false (graph is bipartite or not)
Given N non-negative integers representing an elevation map where the width of each
bar is 1, compute how much water it can trap after raining.
Input:
First line contains a integer N represents the length of array.
Second line contains all N elements of array
Output:
Print total water trapped inside array
Constraints:
1<=N<=2x10^4;
We are given a circular array, print the next greater number for every element. If it is
not found print -1 for that number. To find the next greater number for element Ai ,
start from index i + 1 and go uptil the last index after which we start looking for the
greater number from the starting index of the array since array is circular.
Input:
First line contains the length of the array n. Second line contains the n space separated
integers.
Output:
Print n space separated integers each representing the next greater element.
Example:
Input: 3
1 2 3
Output: 2 3 -1
You are given two non-empty linked lists representing two non-negative integers. The
digits are stored in reverse order, and each of their nodes contains a single digit. Add
the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number
0 itself.
Input:
First line contains length of both linked list n1 and n2.
Second line contains the all n1 elements present in linked list 1
Third line contains the all n2 elements present in linked list 1
Output:
All elements of final linked which contains sum.
Example test case:
Input Output
3 3 023
632
48
Given an array of N elements, where each element is at most K away from its
target position, devise an algorithm that sorts in O(N log K) time.
Input:
First line contains length of array n and k which represent the how much the
array element away from its target position.
Second line contains the all n elements present in array
Output:
All elements of sorted array.
Example test case:
Input Output
6 3 2 3 6 8 12 56
2 6 3 12 56 8
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
Given an integer N, return the nth ugly number.
Example: for 10th ugly number [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first
10 ugly numbers so the 10th ugly number will be 12.
Input:
First line contains a integer N.
Output:
Print the Nth Ugly Number
Constraints:
1<=N<=1690
A binary tree's maximum height is the number of nodes along the longest path from the
root node down to the farthest leaf node.
Input:
First line contains value all Nodes of Tree
Note: all -1 input denotes NULL node
Output:
Return An integer which determine maximum height
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of
every node never differs by more than one.
Input:
First line contains value all Nodes of Tree
Note: all -1 input denotes NULL node
Output:
Return true or false for balanced binary tree
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.
Input:
First line contains value all Nodes of Tree
Note: all -1 input denotes NULL node
Output:
Return the total sum of all root-to-leaf numbers. Test cases are generated so that the
answer will fit in a 32-bit integer.
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the
tree.
The lowest common ancestor is defined between two nodes p and q as the lowest node
in T that has both p and q as descendants (where we allow a node to be a descendant
of itself).
Input:
First line contains value all Nodes of Tree
Note: all -1 input denotes NULL node
Second line contains two integers which represents the value of two nodes p
and q respectively
Output:
Return the value of lowest common ancestor of p and q node
Given an sorted array you need construct a balanced binary tree using
every element of array
Balanced binary tree: difference between left and right sub tree must be
less than or equal to 1
Input:
First line contains a integer N which denotes the size of input sorted array
Output:
Return the level order traversal of constructed binary search tree
Input Output
5 31425
12345
Input Format
First line contains space separated integers representing the node values of the linked list. The list
ends when the input comes as '-1'. The next line contains a single integer k.
Constraints
n < 10^5
Output Format
Output a single line containing the node value at the kth element from last.
Example:
Sample Input
1 2 3 4 5 6 -1
3
Sample Output
Note: The linked list is 1 2 3 4 5 6. -1 is not included in the list. So the third element from the last is
4
Input Format
The First Line contains 3 Integers n, m and k as the Size of the Three LinedLists. Next 3 Lines
contains n, m and k integers Respectively as the elements of Linked Lists. Next Line contains the an
Integer as Target.
Constraints
Output Format
Display the 3 elements from each of the Lists whose sum is equals to the target separated by
space.
Sample Input
3 3 3
12 6 29
23 5 8
90 20 59
101
Sample Output
6 5 90
Explanation
In the Given Sample Input, 6, 5 and 90 from lists 1, 2 and 3 respectively add to give 101.
Maximum length is 4.
Description:
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two
queens attack each other.
For example, the following is a solution for the 4 Queen problem.
The expected output is in the form of a matrix that has ‘Q‘s for the blocks where queens are
placed and the empty spaces are represented by ‘.’ . For example, the following is the output
matrix for the above 4-Queen solution.
INPUT:
First line contains a integer n which represent the number of column and rows in 2D vector
OUTPUT:
First line contains all possible n queen matrix
Example Test case:
INPUT OUTPUT
4 .Q..
...Q
Q...
..Q.
..Q.
Q...
...Q
.Q..
Q19. Write a recursive function which prints subsequences of the array which
sum to target.
Input Format
Take an input N, a number. Take N more inputs and store that in an array. Take an input target (
a number).
Output Format
print the subsets in separate lines.
Constraints
0 < N < 100
Input Output
3 3
123 12
3
Explanation
The sum of all elements in the subsets {3} and {1,2} equals 3.
Q20 .Given an integer 'n'. Print all the possible pairs of 'n' balanced parentheses.
The output strings should be printed in the sorted order considering '(' has higher value than ')'.
Input Format
Single line containing an integral value 'n'.
Output Format
Print the balanced parentheses strings with every possible solution on a new line.
Input Output
2 ()()
(())
Q21.Take as input N, the size of the array. Take N more inputs and store that in an array. Take
as input M, a number. Write a recursive function which returns the last index at which M is found
in the array and -1 if M is not found anywhere. Print the value returned.
Input Format
Enter a number N and add N more numbers to an array, then enter number M to be searched
Output Format
Display the last index at which the number M is found
Sample input:
Input Output
5 3
3
2
1
2
3
2
Output Format
Print the number after replacing all 0's with 5.
Input Output
103 153
Test cases:
Input Output
103 153
10090 15595
100 155
19901 19951
50505050 55555555
Q23.Take as input N, a number. Print odd numbers in decreasing sequence (up until 0) and
even numbers in increasing sequence (up until N) using Recursion
Input Format
Take as input N
Output Format
Print odd numbers in decreasing sequence (up until 0) and even numbers in increasing
sequence (up until N)
Sample test case:
Input Output
5 5
3
1
2
4
24.You are given an N*M grid. Each cell (i,j) in the grid is either blocked, or empty. The rat can
move from a position towards left, right, up or down on the grid.
Initially rat is on the position (1,1). It wants to reach position (N,M) where it's cheese is waiting
for. If a path exists-it is always unique. Find that path and help the rat reach its cheese.
Input Format
First line contains 2 integers N and M denoting the rows and columns in the grid.
Next N line contains M characters each. An 'X' in position (i,j) denotes that the cell is blocked
and ans 'O' denotes that the cell is empty.
Output Format
Print N lines, containing M integers each. A 1 at a position (i,j) denotes that the (i,j)th cell is
covered in the path and a 0 denotes that the cell is not covered in the path.
If a path does not exists then print "NO PATH FOUND"
Input Output
5 4 1 0 0 0
OXOO 1 1 0 0
OOOX 0 1 0 0
XOXO 0 1 1 0
XOOX 0 0 1 1
XXOO
Q23.Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j]
is 1 (or true) then make all the cells of ith row and jth column as 1.
Input Format
First line contains two integers m and n denoting the dimensions of the matrix Next m lines
contain n integers each.
Output Format
Print the modified matrix
Input Output
22 11
10 10
00
24.Given an n x m matrix, where every row and column is sorted in increasing order, and a
number x . Find if element x is present in the matrix or not.
Input Format
First line consists of two space separated integers N and M, denoting the number of element in
a row and column respectively. Second line of each test case consists of N*M space separated
integers denoting the elements in the matrix in row major order. Third line of each test case
contains a single integer x, the element to be searched.
Output Format
Print 1 if the element is present in the matrix, else 0.
Input Output
33 0
3 30 38
44 52 54
57 60 69
62
Explanation
Search the element in the sorted matrix. If the element is present print 1 otherwise print 0. In the
sample input,in first case 62 is not present in the matrix so 0 is printed. Similarly, for second
case 55 is present in the matrix so 1 is printed.
Q25.Given a N*N matrix. The task is to find the index of column with maximum sum. That is the
column whose sum of elements are maximum.
Input Format
First line contains the N ,size of the square matrix. Next N lines contains N integers each
denoting the elements of the matrix
Output Format
Print N lines each containing N elements. These are the elements of the new matrix.
Sample Input
Input Output
7 6 341
90 40 1 3 39 59 90
48 72 67 32 73 19 27
22 37 47 68 1 5 55
81 5 39 53 38 86 21
1 32 7 44 2 65 47
68 13 24 28 69 81 43
16 34 67 3 82 26 35
Explanation:
6th column has the highest sum that is 341.
Input Format
First line contains a single integer N. Next N lines contain N space separated integers.
Output Format
Print N lines with N space separated integers of the rotated array.
Input Output
4 4 8 12 16
1234 3 7 11 15
5678 2 6 10 14
9 10 11 12 1 5 9 13
13 14 15 16
Q27.Take an input N, a number. Take N more inputs and store that in an array. Take an input
target, a number
a. Write a recursive function which prints subsets of the array which sum to target.
b. Write a recursive function which counts the number of subsets of the array which sum to
target. Print the value returned.
Input Format
Take an input N, a number. Take N more inputs and store that in an array. Take an input target,
a number
Output Format
Display the number of subsets and print the subsets in a space separated manner.
Input Output
3 12 3
1 2
2
3
3
Q28.Take as input S, a string. Write a function that toggles the case of all characters in the
string. Print the value returned.
Input Format
String
Output Format:
String
Sample Input:
Input Output
abC ABc
Explanation
Toggle Case means to change UpperCase character to LowerCase character and vice-versa.
Q29.Take as input S, a string. Write a function that replaces every even character with the
character having just higher ASCII code and every odd character with the character having just
lower ASCII code. Print the value returned.
Input Format
String
Output Format
String
Input Output
abcg badf
Q30.Take as input S, a string. Write a function that returns true if the string is a palindrome and
false otherwise. Print the value returned.
Input Format
String
Output Format
Boolean
Sample input:
Input Output
abba true
Explanation
A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is
palindrome as it's reverse is "abba", but “abbc” is not palindrome as it's reverse is "cbba".
31.Take as input S, a string. Write a function that returns the character with maximum frequency.
Print the value returned.
Input Format
String
Output Format
Character
Sample Input
Input Output
aaabacb a
Explanation
For the given input string, a appear 4 times. Hence, it is the most frequent character.
Q32.Take as input a 2-d array. Print the 2-D array in spiral form anti-clockwise.
Input Format
Two integers M(row) and N(column) and further M * N integers(2-d array numbers).
Output Format
All M * N integers separated by commas with 'END' written in the end(as shown in example).
Sample Input
Input Output
44 11, 21, 31, 41, 42, 43, 44, 34, 24, 14, 13,
11 12 13 14 12, 22, 32, 33, 23, END
21 22 23 24
31 32 33 34
41 42 43 44
Explanation
For spiral level anti-clockwise traversal, Go for first column-> last row ->last column-> first row
and then do the same traversal for the remaining matrix .
Q33. Unique Paths
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e.,
grid[0][0]).
The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only
move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can
take to reach the bottom-right corner.
Input Format
Two integers M(row) and N(column).
Output Format
Print the total number of unique paths can be formed
Sample Input
Input Output
45 35
The test cases are generated so that the answer will be less than or equal to 2 * 10^9
You are given an array , your task is to rearrange the whole array so it can form a
max heap order
Input Format
First line contains an Integer N which represents the size of array.
Output Format
Print the max heap order of the array .
Sample Input
Input Output
8 10 7 9 3 4 1 5 2
2 4 1 7 10 9 5 3
You may assume that you have an infinite number of each kind of coin.
Input Format
First line contains Two Integers ‘N’ which represents the size of coins array and ‘Amount’ which
represents the total money you need to make .
Output Format
Print the fewest number of coins that you need to make up that amount. If that amount
of money cannot be made up by any combination of the coins, Print -1.
Sample Input
Input Output
4 12 3
2351
You are a professional robber planning to rob houses along a street. Each house has a certain
amount of money stashed, the only constraint stopping you from robbing each of them is that
adjacent houses have security systems connected and it will automatically contact the police if
two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house,
Input Format
First line contains a Integers ‘N’ which represents the size of house array.
Output Format
Print the maximum amount of money you can rob tonight without alerting the police
Sample Input
Input Output
5 9
14235
Note : As the answer might be large, return the final answer modulo 10^9 + 7
Input Format
Sample Input
Input Output
You are given an integer array nums. You are initially positioned at the array's first index, and
each element in the array represents your maximum jump length at that position.
Input Format
First line contains a Integers ‘N’ which represents the size of nums array.
Output Format
Print true if you can reach the last index, or false otherwise.
Sample Input
Input Output
5 true
23114