Dsa Q
Dsa Q
Dsa Q
You are given an array arr(0-based index). The size of the array is given by sizeOfArray. You need
to insert an element at given index.
Example 1:
Input:
sizeOfArray = 6
arr[] = {1, 2, 3, 4, 5}
index = 5, element = 90
Output: 1 2 3 4 5 90
Explanation: 90 is inserted at index
5(0-based indexing). After inserting,
array elements are like
1, 2, 3, 4, 5, 90.
Example 2:
Input:
sizeOfArray = 6
arr[] = {1, 2, 3, 4, 5}
index = 2, element = 90
Output: 1 2 90 3 4 5
Explanation: 90 is inserted at index
2(0-based indexing). After inserting,
array elements are like
1, 2, 90, 3, 4, 5.
Your Task:
You don't need to read input or print anything.. The input is already taken care of by the driver
code. You only need to complete the function insertAtIndex() that takes arr, sizeOfArray, index,
element as input and modifies the array arr as per requirements. The printing is done by driver
code.
Constraints:
1 <= sizeOfArray <= 10000
0 <= element, arri <= 106
0 <= index <= sizeOfArray-1
Q2 Array insert at end
Insertion is a basic but frequently used operation. Arrays in most languages can not be
dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an
element at the end of the array.
You are given an array arr. The size of the array is given by sizeOfArray. You need to insert an
element at the end.
Array already have the sizeofarray -1 elements.
Example 1:
Input:
sizeOfArray = 6
arr[] = {1, 2, 3, 4, 5}
element = 90
Output: 1 2 3 4 5 90
Explanation: After inserting 90 at the
end, we have array elements as
1 2 3 4 5 90.
Example 2:
Input:
sizeOfArray = 4
arr[] = {1, 2, 3}
element = 50
Output: 1 2 3 50
Explanation: After inserting 50 at the
end, we have array elements as
1 2 3 50.
Your Task:
You don't need to read input or print anything. You only need to complete the function
insertAtEnd() that takes arr, sizeOfArray, element as input and modifies arr as per requirements.
The driver code takes care of the printing.
Constraints:
1 <= sizeOfArray <= 1000
0 <= element, arri <= 106
Q3 You are given an array of size n. Find the maximum possible length of a subarray such that its
elements are arranged alternately either as even and odd or odd and even .
Example 1:
Input:
n=5
a[] = {10,12,14,7,8}
Output: 3
Explanation: The max length of subarray
is 3 and the subarray is {14 7 8}. Here
the array starts as an even element and
has odd and even elements alternately.
Example 2:
Input:
n=2
a[] = {4,6}
Output: 1
Explanation: The array contains {4 6}.
So, we can only choose 1 element as
that will be the max length subarray.
Your Task:
You don't need to take any input. Just complete the function maxEvenOdd() that returns the
maximum length.
Constraints:
1 <= n <= 106
1 <= Ai <= 103
Searching
Q1 Given an integer array and another integer element. The task is to find if the given element is
present in array or not.
Example 1:
Input:
n=4
arr[] = {1,2,3,4}
x=3
Output: 2
Explanation: There is one test case
with array as {1, 2, 3 4} and element
to be searched as 3. Since 3 is
present at index 2, output is 2.
Example 2:
Input:
n=5
arr[] = {1,2,3,4,5}
x=5
Output: 4
Explanation: For array elements
{1,2,3,4,5} element to be searched
is 5 and it is at index 4. So, the
output is 4.
Your Task:
The task is to complete the function search() which takes the array arr[], its size N and the
element X as inputs and returns the index of first occurrence of X in the given array. If the
element X does not exist in the array, the function should return -1.
Constraints:
1 <= n <= 106
0 <= arr[i] <= 106
0 <= x <= 105
Q2 Square root of a number
Given an integer x, find the square root of x. If x is not a perfect square, then return floor(√x).
Example 1:
Input:
x=5
Output: 2
Explanation: Since, 5 is not a perfect
square, floor of square_root of 5 is 2.
Example 2:
Input:
x=4
Output: 2
Explanation: Since, 4 is a perfect
square, so its square root is 2.
Your Task:
You don't need to read input or print anything. The task is to complete the function floorSqrt()
which takes x as the input parameter and return its square root.
Note: Try Solving the question without using the sqrt function. The value of x>=0.
Constraints:
1 ≤ x ≤ 107
In case of multiple subarrays, return the subarray indexes which come first on moving from left
to right.
Note:- You have to return an ArrayList consisting of two elements left and right. In case no such
subarray exists return an array consisting of element -1.
Example 1:
Input:
N = 5, S = 12
A[] = {1,2,3,7,5}
Output: 2 4
Explanation: The sum of elements
from 2nd position to 4th position
is 12.
Example 2:
Input:
N = 10, S = 15
A[] = {1,2,3,4,5,6,7,8,9,10}
Output: 1 5
Explanation: The sum of elements
from 1st position to 5th position
is 15.
Your Task:
You don't need to read input or print anything. The task is to complete the function
subarraySum() which takes arr, N, and S as input parameters and returns an ArrayList containing
the starting and ending positions of the first such occurring subarray from the left where sum
equals to S. The two indexes in the array should be according to 1-based indexing. If no such
subarray is found, return an array consisting of only one element that is -1.
Constraints:
1 <= N <= 105
0 <= Ai <= 109
0<= S <= 109
Q4 You are given an array arr[] of N integers. The task is to find the smallest positive number
missing from the array.
Note: Positive number starts from 1.
Example 1:
Input:
N=5
arr[] = {1,2,3,4,5}
Output: 6
Explanation: Smallest positive missing
number is 6.
Example 2:
Input:
N=5
arr[] = {0,-10,1,3,-20}
Output: 2
Explanation: Smallest positive missing
number is 2.
Your Task:
The task is to complete the function missingNumber() which returns the smallest positive
missing number in the array.
Constraints:
1 <= N <= 106
-106 <= arr[i] <= 106
Sorting
Q1 Bubble Sort
EasyAccuracy
Given an Integer N and a list arr. Sort the array using bubble sort algorithm.
Example 1:
Input:
N=5
arr[] = {4, 1, 3, 9, 7}
Output:
13479
Example 2:
Input:
N = 10
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Output:
1 2 3 4 5 6 7 8 9 10
Your Task:
You don't have to read input or print anything. Your task is to complete the function bubblesort()
which takes the array and it's size as input and sorts the array using bubble sort algorithm.
Constraints:
1 <= N <= 103
1 <= arr[i] <= 103
Q2 The task is to complete the insert() function which is used to implement Insertion Sort.
Example 1:
Input:
N=5
arr[] = { 4, 1, 3, 9, 7}
Output:
13479
Example 2:
Input:
N = 10
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Output:
1 2 3 4 5 6 7 8 9 10
Your Task:
You don't have to read input or print anything. Your task is to complete the function insert() and
insertionSort() where insert() takes the array, it's size and an index i and insertionSort() uses
insert function to sort the array in ascending order using insertion sort algorithm.
Constraints:
1 <= N <= 1000
1 <= arr[i] <= 1000
Q3 Merge three sorted arrays
Given three sorted arrays A, B and C of size N, M and P respectively. The task is to merge them
into a single array which must be sorted in increasing order.
Example 1:
Input:
N = 4, A[] = [1 2 3 4]
M = 5, B[] = [1 2 3 4 5]
P = 6, C[] = [1 2 3 4 5 6]
Output: 1 1 1 2 2 2 3 3 3 4 4 4 5 5 6
Explanation: Merging these three sorted
arrays, we have:
1 1 1 2 2 2 3 3 3 4 4 4 5 5 6.
Example 2:
Input:
N = 2, A[] = [1 2]
M = 3, B[] = [2 3 4]
P = 4, C[] = [4 5 6 7]
Output: 1 2 2 3 4 4 5 6 7
Explanation: Merging three sorted arrays,
we have: 1 2 2 3 4 4 5 6 7.
Your Task:
This is a function problem. You only need to complete the function mergeThree() that takes
A,B,C as parameters. The function returns an array or vector.
Constraints:
1 <= N, M, P <= 106
1 <= A[i], B[i], C[i] <= 106
Q4 Given an array arr[] of n integers. Check whether it contains a triplet that
sums up to zero.
Note: Return 1, if there is at least one triplet following the condition else
return 0.
Example 1:
Your Task:
You don't need to read input or print anything. Your task is to complete the
boolean function findTriplets() which takes the array arr[] and the size of the
array (n) as inputs and print 1 if the function returns true else print 0 if the
function returns false.
Constraints:
1 <= n <= 104
-106 <= Ai <= 106
STRING
Q1 Given a binary string S. The task is to count the number of substrings that
start and end with 1. For example, if the input string is “00100101”, then there
are three substrings “1001”, “100101” and “101”.
Example 1:
Input:
N=4
S = 1111
Output: 6
Explanation: There are 6 substrings from
the given string. They are 11, 11, 11,
111, 111, 1111.
Example 2:
Input:
N=5
S = 01101
Output: 3
Explanation: There 3 substrings from the
given string. They are 11, 101, 1101.
Your Task:
The task is to complete the function binarySubstring() which takes the length
of binary string n and a binary string a as input parameter and counts the
number of substrings starting and ending with 1 and returns the count.
Constraints:
1 ≤ |S| ≤ 104
Q2 Isomorphic Strings
EasyAccuracy: 34.21%Submissions: 174K+Points: 2
Given two strings 'str1' and 'str2', check if these two strings are isomorphic to each other.
If the characters in str1 can be changed to get str2, then two strings, str1 and str2, are
isomorphic. A character must be completely swapped out for another character while
maintaining the order of the characters. A character may map to itself, but no two characters
may map to the same character.
Example 1:
Input:
str1 = aab
str2 = xxy
Output:
1
Explanation:
There are two di erent characters in aab and xxy, i.e a and b with frequency 2 and 1 respectively.
Example 2:
Input:
str1 = aab
str2 = xyz
Output:
0
Explanation:
There are two di erent characters in aab but there are three di erent charactersin xyz. So there
won't be one to one mapping between str1 and str2.
Your Task:
You don't need to read input or print anything.Your task is to complete the function
areIsomorphic() which takes the string str1 and string str2 as input parameter and check if two
strings are isomorphic. The function returns true if strings are isomorphic else it returns false.
Constraints:
1 <= |str1|, |str2| <= 105
Q3 Check if a string is Isogram or not
BasicAccuracy: 63.25%Submissions: 54K+Points: 1
Given a string S of lowercase alphabets, check if it is isogram or not. An Isogram is a string in
which no letter occurs more than once.
Example 1:
Input:
S = machine
Output: 1
Explanation: machine is an isogram
as no letter has appeared twice. Hence
we print 1.
Example 2:
Input:
S = geeks
Output: 0
Explanation: geeks is not an isogram
as 'e' appears twice. Hence we print 0.
Your Task:
This is a function problem. You only need to complete the function isIsogram() that takes a
string as a parameter and returns either true or false.
Constraints:
1 <= |s| <= 103
Q4 Pangram Checking
EasyAccuracy: 61.34%Submissions: 74K+Points: 2
Given a string s check if it is "Panagram" or not.
Example 1:
Input:
s = "Bawds jog, flick quartz, vex nymph"
Output:
1
Explanation:
In the given input, there
are all the letters of the English
alphabet. Hence, the output is 1.
Example 2:
Input:
s = "sdfs"
Output:
0
Explanation:
In the given input, there
aren't all the letters present in the
English alphabet. Hence, the output
is 0.
Your Task:
You do not have to take any input or print anything. You need to complete the function
checkPangram() that takes a string as a parameter and returns true if the string is a Panagram,
or else it returns false.
Constraints:
1 ≤ |s| ≤ 104
Both Uppercase & Lowercase are considerable
Linked list
Example 1:
Input:
LinkedList: 1->2->3->4->5
Output: 5
Explanation: Count of nodes in the
linked list is 5, which is its length.
Example 2:
Input:
LinkedList: 2->4->6->7->5->1->0
Output: 7
Explanation: Count of nodes in the
linked list is 7. Hence, the output
is 7.
Your Task:
Your task is to complete the given function getCount(), which takes a head reference as an
argument and should return the length of the linked list.
Constraints:
1 <= N <= 105
1 <= value <= 103
Q2 Count nodes of linked list
BasicAccuracy: 85.99%Submissions: 152K+Points: 1
Given a singly linked list. The task is to find the length of the linked list, where
length is defined as the number of nodes in the linked list.
Example 1:
Input:
LinkedList: 1->2->3->4->5
Output: 5
Explanation: Count of nodes in the
linked list is 5, which is its length.
Example 2:
Input:
LinkedList: 2->4->6->7->5->1->0
Output: 7
Explanation: Count of nodes in the
linked list is 7. Hence, the output
is 7.
Your Task:
Your task is to complete the given function getCount(), which takes a head
reference as an argument and should return the length of the linked list.
Constraints:
1 <= N <= 105
1 <= value <= 103
Add two numbers represented by linked lists
MediumAccuracy: 34.52%Submissions: 231K+Points: 4
Given two decimal numbers, num1 and num2, represented by linked lists of size n and m
respectively. The task is to return a linked list that represents the sum of these two numbers.
For example, the number 190 will be represented by the linked list, 1->9->0->null, similarly 25 by
2->5->null. Sum of these two numbers is 190 + 25 = 215, which will be represented by 2->1->5-
>null. You are required to return the head of the linked list 2->1->5->null.
Note: There can be leading zeros in the input lists, but there should not be any leading zeros in
the output list.
Example 1:
Input:
n=2
num1 = 45 (4->5->null)
m=3
num2 = 345 (3->4->5->null)
Output:
3->9->0->null
Explanation:
For the given two linked list (4 5) and (3 4 5), after adding the two linked list resultant linked list
will be (3 9 0).
Example 2:
Input:
n=4
num1 = 0063 (0->0->6->3->null)
m=2
num2 = 07 (0->7->null)
Output:
7->0->null
Explanation:
For the given two linked list (0 0 6 3) and (0 7), after adding the two linked list resultant linked list
will be (7 0).
Your Task:
The task is to complete the function addTwoLists() which has node reference of both the linked
lists and returns the head of the sum list.
Constraints:
1 <= n, m <= 104
Stack
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3, 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3,
the stack will be {2}
push(4) the stack will be {2 4}
pop() poped element will be 4
Example 2:
Input:
pop()
push(4)
push(5)
pop()
Output: -1, 5
Your Task:
You are required to complete two methods push() and pop(). The push() method takes one
argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present
at the top and popped out from the stack. If the stack is empty then return -1 from the pop()
method.
Constraints:
1 <= Q <= 100
1 <= x <= 100
Implement Stack using Linked List
BasicAccuracy: 53.98%Submissions: 123K+Points: 1
Let's give it a try! You have a linked list and you have to implement the functionalities push and
pop of stack using this given linked list. Your task is to use the class as shown in the comments
in the code editor and complete the functions push() and pop() to implement a stack.
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3,
the stack will be {2}
push(4) the stack will be {2 4}
pop() poped element will be 4
Example 2:
Input:
pop()
push(4)
push(5)
pop()
Output: -1 5
Your Task: You are required to complete two methods push() and pop(). The push() method
takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer
present at the top and popped out from the stack. If the stack is empty then return -1 from the
pop() method.
Constraints:
1 <= Q <= 100
1 <= x <= 100
Delete middle element of a stack
EasyAccuracy: 53.71%Submissions: 127K+Points: 2
Given a stack, delete the middle element of the stack without using any additional data
structure.
Middle element:- floor((size_of_stack+1)/2) (1-based indexing) from bottom of the stack.
Note: The output shown by the compiler is the stack from top to bottom.
Example 1:
Input:
Stack = {10, 20, 30, 40, 50}
Output:
ModifiedStack = {10, 20, 40, 50}
Explanation:
If you print the stack the bottom-most element will be 10 and the top-most element will be 50.
Middle element will be element at index 3 from bottom, which is 30. Deleting 30, stack will look
like {10 20 40 50}.
Example 2:
Input:
Stack = {10 20 30 40}
Output:
ModifiedStack = {10 30 40}
Explanation:
If you print the stack the bottom-most element will be 10 and the top-most element will be 40.
Middle element will be element at index 2 from bottom, which is 20. Deleting 20, stack will look
like {10 30 40}.
Your Task:
You don't need to read input or print anything. Complete the function deleteMid() which takes
the stack and its size as input parameters and modifies the stack in-place.
Constraints:
2 ≤ size of stack ≤ 105
Implement two stacks in an array
EasyAccuracy: 56.49%Submissions: 150K+Points: 2
Your task is to implement 2 stacks in one array e iciently. You need to implement 4 methods.
twoStacks : Initialize the data structures and variables to be used to implement 2 stacks in one
array.
push1 : pushes element into first stack.
push2 : pushes element into second stack.
pop1 : pops element from first stack and returns the popped element. If first stack is empty, it
should return -1.
pop2 : pops element from second stack and returns the popped element. If second stack is
empty, it should return -1.
Example 1:
Input:
push1(2)
push1(3)
push2(4)
pop1()
pop2()
pop2()
Output:
3 4 -1
Explanation:
push1(2) the stack1 will be {2}
push1(3) the stack1 will be {2,3}
push2(4) the stack2 will be {4}
pop1() the poped element will be 3 from stack1 and stack1 will be {2}
pop2() the poped element will be 4 from stack2 and now stack2 is empty
pop2() the stack2 is now empty hence returned -1.
Example 2:
Input:
push1(1)
push2(2)
pop1()
push1(3)
pop1()
pop1()
Output:
1 3 -1
Explanation:
push1(1) the stack1 will be {1}
push2(2) the stack2 will be {2}
pop1() the poped element will be 1 from stack1 and stack1 will be empty
push1(3) the stack1 will be {3}
pop1() the poped element will be 3 from stack1 and stack1 will be empty
pop1() the stack1 is now empty hence returned -1.
Your Task:
You don't need to read input or print anything. You are required to complete the 4 methods
push1, push2 which takes one argument an integer 'x' to be pushed into stack one and two and
pop1, pop2 which returns the integer poped out from stack one and two. If no integer is present
in the stack return -1.
Constraints:
1 <= Number of queries <= 104
1 <= Number of elements in the stack <= 100
The sum of count of elements in both the stacks < size of the given array
Queue
Example 1:
Input:
Q=5
Queries = 1 2 1 3 2 1 4 2
Output: 2 3
Explanation:
In the first test case for query
1 2 the queue will be {2}
1 3 the queue will be {2 3}
2 poped element will be 2 the
queue will be {3}
1 4 the queue will be {3 4}
2 poped element will be 3
Example 2:
Input:
Q=4
Queries = 1 3 2 2 1 4
Output: 3 -1
Explanation:
In the second testcase for query
1 3 the queue will be {3}
2 poped element will be 3 the
queue will be empty
2 there is no element in the
queue and hence -1
1 4 the queue will be {4}.
Your Task :
You are required to complete the two methods push() which take one argument an integer 'x' to
be pushed into the queue and pop() which returns a integer poped out from othe queue. If the
queue is empty, it should return -1 on a pop operation.
Constraints:
1 ≤ Q ≤ 105
0 ≤ x ≤ 105
Operations on Queue
BasicAccuracy: 58.39%Submissions: 19K+Points: 1
Given a queue of integers and Q queries. The task is to perform operations on queue according
to the query.
f y : (check if the element y is present or not in the queue). Return "Yes" if present, else "No".
Example 1:
Input:
Q=6
Queries = i 2 i 4 i 3 i 5 h f 8
Output:
2
No
Explanation: Inserting 2, 4, 3, and 5
onto the queue: 2 4 3 5. h means front
So front is 2. f is find. 8 is not in
queue so No.
Example 2:
Input:
Q=4
Queries = i 3 i 4 r f 3
Output: No
Explanation: Inserting 3 and 4 . When
we return and remove 3 and then when
we find 3 , it will return NO as
output as 3 is not present in the
queue.
Your Task:
Your task is to complete functions enqueue(), dequeue(), front() and find() which performs the
operations described above in the problem description.
Expected Time Complexity: O(1) for enqueue(), dequeue() and front(); O(N) for find().
Expected Auxiliary Space: O(1) for all the 4 functions.
Constraints:
1 <= Q <= 103
Example 1:
Input:
53
12345
Output:
32145
Explanation:
After reversing the given
input from the 3rd position the resultant
output will be 3 2 1 4 5.
Example 2:
Input:
44
4321
Output:
1234
Explanation:
After reversing the given
input from the 4th position the resultant
output will be 1 2 3 4.
Your Task:
Complete the provided function modifyQueue() that takes queue and K as parameters and
returns a modified queue. The printing is done automatically by the driver code.
Constraints:
1 <= K <= N <= 105
Circular tour
MediumAccuracy: 34.79%Submissions: 161K+Points: 4
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of
data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without
exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N=4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns
an integer denoting a point from where a truck will be able to complete the circle (The truck will
stop at each petrol pump and it has infinite capacity). If there exists multiple such starting
points, then the function must return the first one out of those. (return -1 otherwise)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
Graph
Q1 Given an undirected graph with V nodes and E edges, create and return
an adjacency list of the graph. 0-based indexing is followed everywhere.
Example 1:
Input:
V = 5, E = 7
edges = {(0,1),(0,4),(4,1),(4,3),(1,3),(1,2),(3,2)}
Output:
{{1,4},
{0,2,3,4},
{1,3},
{1,2,4},
{0,1,3}}
Explanation:
Node 0 is connected to 1 and 4.
Node 1 is connected to 0,2,3 and 4.
Node 2 is connected to 1 and 3.
Node 3 is connected to 1,2 and 4.
Node 4 is connected to 0,1 and 3.
Example 2:
Input:
V = 4, E = 3
edges = {(0,3),(0,2),(2,1)}
Output:
{{2,3}
{2},
{0,1}
{0}}
Explanation:
Node 0 is connected to 2 and 3.
Node 1 is only connected to 2.
Node 2 is connected to 0 and 1.
Node 3 is only connected to 0.
Your task:
You don't need to read input or print anything. Your task is to complete the
function printGraph() which takes the integer V denoting the number of
vertices and edges as input parameters and returns the list of list denoting the
adjacency list.
Constraints:
1 ≤ V, E ≤ 105
BFS of graph
EasyAccuracy: 44.09%Submissions: 347K+Points: 2
Given a directed graph. The task is to do Breadth First Traversal of this graph
starting from 0.
Note: One can move from node u to node v only if there's an edge from u to v.
Find the BFS traversal of the graph starting from the 0th vertex, from left to
right according to the input graph. Also, you should only take nodes directly or
indirectly connected from Node 0 in consideration.
Example 1:
Input:
V = 5, E = 4
adj = {{1,2,3},{},{4},{},{}}
Output:
01234
Explanation:
0 is connected to 1 , 2 , 3.
2 is connected to 4.
so starting from 0, it will go to 1 then 2
then 3. After this 2 to 4, thus bfs will be
0 1 2 3 4.
Example 2:
Input:
V = 3, E = 2
adj = {{1,2},{},{}}
Output:
012
Explanation:
0 is connected to 1 , 2.
so starting from 0, it will go to 1 then 2,
thus bfs will be 0 1 2.
Your task:
You dont need to read input or print anything. Your task is to complete the
function bfsOfGraph() which takes the integer V denoting the number of
vertices and adjacency list as input parameters and returns a list containing
the BFS traversal of the graph starting from the 0th vertex from left to right.
Constraints:
1 ≤ V, E ≤ 104
DFS of Graph
You are given a connected undirected graph. Perform a Depth First Traversal
of the graph.
Note: Use the recursive approach to find the DFS traversal of the graph
starting from the 0th vertex from left to right according to the graph.
Example 1:
Output: 0 2 4 3 1
Explanation:
0 is connected to 2, 3, 1.
1 is connected to 0.
2 is connected to 0 and 4.
3 is connected to 0.
4 is connected to 2.
so starting from 0, it will go to 2 then 4,
and then 3 and 1.
Thus dfs will be 0 2 4 3 1.
Example 2:
Output: 0 1 2 3
Explanation:
0 is connected to 1 , 3.
1 is connected to 0, 2.
2 is connected to 1.
3 is connected to 0.
so starting from 0, it will go to 1 then 2
then back to 0 then 0 to 3
thus dfs will be 0 1 2 3.
Your task:
You don't need to read input or print anything. Your task is to complete the
function dfsOfGraph() which takes the integer V denoting the number of
vertices and adjacency list as input parameters and returns a list containing
the DFS traversal of the graph starting from the 0th vertex from left to right
according to the graph.
Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V) Constraints:
1 ≤ V, E ≤ 104
Find the number of islands
MediumAccuracy: 42.12%Submissions: 177K+Points: 4
Given a grid of size n*m (n is the number of rows and m is the number of
columns in the grid) consisting of '0's (Water) and '1's(Land). Find the
number of islands.
Example 1:
Input:
grid = {{0,1},{1,0},{1,1},{1,0}}
Output:
1
Explanation:
The grid is-
01
10
11
10
All lands are connected.
Example 2:
Input:
grid = {{0,1,1,1,0,0,0},{0,0,1,1,0,1,0}}
Output:
2
Expanation:
The grid is-
0111000
0011010
There are two islands :- one is colored in blue
and other in orange.
Your Task:
You don't need to read or print anything. Your task is to complete the
function numIslands() which takes the grid as an input parameter and
returns the total number of islands.
Constraints:
1 ≤ n, m ≤ 500
Hashing
Given an array of integers and a hash table size. Fill the array elements into a
hash table using Linear Probing to handle collisions. Duplicate elements must
be mapped to the same position in the hash table while colliding elements
must be mapped to the [(value+1)%hashSize] position.
Note :- If there's no more space to insert a new element, just drop that
element.
Example 1:
Input:
hashSize = 10
sizeOfArray = 4
Array[] = {4,14,24,44}
Output:
-1 -1 -1 -1 4 14 24 44 -1 -1
Explanation: 4%10=4. So put 4 in
hashtable[4].Now, 14%10=4, but
hashtable[4] is alreadyfilled so put
14 in the next slot and so on.
Example 2:
Input:
hashSize = 10
sizeOfArray = 4
Array[] = {9,99,999,9999}
Output:
99 999 9999 -1 -1 -1 -1 -1 -1 9
Explanation: 9%10=9. So put 9 in
hashtable[9]. Now, 99%10=9, but
hashtable[9] is already filled so
put 99 in the (99+1)%10 =0 slot so
99 goes into hashtable[0] and so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the
function linearProbing() which takes the hash table size (HashSize), an
integers array arr[], and its size N as input parameters and inserts all the
elements of the array arr[] into a hash table. The function should return the
hash table.
The empty cells of the hash table are to be given a value of -1.
Also, if there's no more space to insert a new element, just drop that element.
Constraints:
1 <= hashSize <= 1000
1 <= sizeOfArray <= 10000
0 <= Array[] <= 105
Separate chaining in Hashing
Separate chaining technique in hashing allows to us to use a linked list at each
hash slot to handle the problem of collisions. That is, every slot of the hash table
is a linked list, so whenever a collision occurs, the element can be appened as a
node to the linked list at the slot.
In this question, we'll learn how to fill up the hash table using Separate
chaining technique. Given an array (consisting of distinct integers) and a
hashtable size, you have to fill the elements of the array into a hash table
of given size.
Example 1:
Input:
hashSize = 10
sizeOfArray = 6
arr[] = {92,4,14,24,44,91}
Output:
1->91
2->92
4->4->14->24->44
Explanation: 92%10=2 so 92 goes to slot 2.
4%10=4 so 4 goes to slot 4. 14%10=4. But 4
is already occupied so we make a linked
list at this position and add 14 after 4
in slot 4 and so on.
Example 2:
Input:
hashSize = 10
sizeOfArray = 5
arr[] = {12,45,36,87,11}
Output:
1->11
2->12
5->45
6->36
7->87
Explanation: 12%10=2 so 12 goes to slot 2.
45%10=5 goes to slot 5. 36%10=6 goes to
slot 6. 87%10=7 goes to slot 7 and finally
11%10=1 goes to slot 1.
Your Task:
This is a function problem. You need to complete the
function separateChaining that takes hashSize, arr, and sizeOfArr as
parameters, inserts elements of arr in the hashTable at positions by
using arr[i]%hashSize and then returns the has table. The printing is
done automatically by the driver code.
Constraints:
2 <= hashSize <= 103
1 <= sizeOfArray <= 103
0 <= arri <= 107
Sorting Elements of an Array by Frequency
MediumAccuracy: 44.93%Submissions: 49K+Points: 4
Given an array of integers, sort the array according to frequency of elements. That is elements
that have higher frequency come first. If frequencies of two elements are same, then smaller
number comes first.
Example 1:
Input:
N=5
A[] = {5,5,4,6,4}
Output: 4 4 5 5 6
Explanation: The highest frequency here is
2. Both 5 and 4 have that frequency. Now
since the frequencies are same then
smallerelement comes first. So 4 4 comes
firstthen comes 5 5. Finally comes 6.
The output is 4 4 5 5 6.
Example 2:
Input:
N=5
A[] = {9,9,9,2,5}
Output: 9 9 9 2 5
Explanation: The highest frequency here is
3. The element 9 has the highest frequency
So 9 9 9 comes first. Now both 2 and 5
have same frequency. So we print smaller
element first.
The output is 9 9 9 2 5.
Your Task:
You only need to complete the function sortByFreq that takes arr, and n as parameters and
returns the sorted array.
Constraints:
1 ≤ N ≤ 105
1 ≤ Ai ≤ 105
Tree
Preorder Traversal
Example 1:
Input:
1
/
4
/ \
4 2
Output: 1 4 4 2
Example 2:
Input:
6
/ \
3 2
\ /
12
Output: 6 3 1 2 2
Your Task:
You just have to complete the function preorder() which takes the root node
of the tree as input and returns an array containing the preorder traversal of
the tree.
Constraints:
1 <= Number of nodes <= 104
0 <= Data of a node <= 105
Inorder Traversal
Given a Binary Tree, find the In-Order Traversal of it.
Example 1:
Input:
1
/ \
3 2
Output: 3 1 2
Example 2:
Input:
10
/ \
20 30
/ \ /
40 60 50
Output: 40 20 60 10 50 30
Your Task:
You don't need to read input or print anything. Your task is to complete the
function inOrder() that takes root node of the tree as input and returns a list
containing the In-Order Traversal of the given Binary Tree.
Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 105
Postorder Traversal
Given a binary tree, find the Postorder Traversal of it.
For Example, the postorder traversal of the following tree is:
5 10 39 1
1
/ \
10 39
/
5
Example 1:
Input:
19
/ \
10 8
/ \
11 13
Output: 11 13 10 8 19
Example 2:
Input:
11
/
15
/
7
Output: 7 15 11
Your Task:
You don't need to read input or print anything. Your task is to complete the
function postOrder() that takes root node as input and returns an array
containing the postorder traversal of the given Binary Tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 106