CODING QUESTION 10marks
CODING QUESTION 10marks
Problem Statement: Write a C++ program to perform addition, subtraction, multiplication, and
division of two numbers. Take the input in main function and call the function by reference and
return the result to main function. Result must be printed in main function. You have to define
four functions; add(), sub(), mul() and divi().
Input:15 3
Output:18 12 45 5
Question 2:
Problem Statement:Hemant is frustrated about multiplying two matrix A[m][n] and B[n][p].
The user should enter the dimension and elements of the matrix at runtime. Print the resultant
matrix. The program will also check the column size of first matrix and row size of second
matrix. If they are not equal, it will ask the user to enter the dimension again.
Input:
2 3 (rows and cols of first matrix)
3 2 (rows and cols of second matrix)
1 2 3 4 5 6 (elements of first matrix)
10 11 20 21 30 31 (elements of second matrix)
Question 3:
Problem Statement: Write a C++ program to merge two sorted array of different size. The
resultant array should also be in sorted order.
Example:
Input: arr1 = {1, 3, 4, 5}, arr2 = {2, 4, 6, 8}
Output: arr3 = {1, 2, 3, 4, 4, 5, 6, 8}
Input:
3 (Length of first array)
3 (Length of second array)
589
478
Output:4 5 7 8 8 9
Question 4:
Problem Statement:
Write a C++ program to input an integer array of size 10. The elements should be inserted
randomly. Calculate;
a. Average of all elements
b. Find largest and smallest element
c. Find the sum of all even numbers
Input: 1 2 3 4 5 6 7 8 9 10
Output:
5
1
10
30
Question 5:
Problem Statement:
Manoj is playing with an array with n distinct elements and trying to find all elements in that
array which have at-least two greater elements than themselves.
Input
In first line, there will be an integer (N) representing number of elements. In next line, there will
be N integers representing value of each element
Output
List of elements in a single row
Examples :
Input :
5
28715
Output :
2 1 5
Explanation:
The output three elements have two or more greater elements
Question 6:
Problem Statement:
Sudhir is frustrated with manual calculation of transpose of a matrix.Help Sudhir, writing C++
program to calculate transpose of a matrix. Transpose of a matrix is obtained by changing rows
to columns and columns to rows
INPUT
1 2 3
4 5 6
7 8 9
Output
1 4 7
2 5 8
3 6 9
Question 7:
Problem Statement:
Even number of Students ‘N’ were standing in the hostel mess queue Q(Q1, Q2, Q3, Q4,...., Qn)
and they were waiting for food to put on a buffet table.
In the meantime, the warden came on the inspection and wanted to engage with the students for
one to one feedback about food.
To make it fair, he asked the students to make two queues X(Q1, Q2, Q3, Q4…), Y(Q5….Qn)
by dividing it in half. And then the warden started calling one student from each queue.
After attending to warden, students get back to queue Q’ for food but because the warden is
interacting with student from both queues one by one therefore the final queue of students is
interleaving the position of the previous queue(Q1, Q5, Q3, Q4……, Qn).
You are asked to write a program to automate this process for the convenience of both the
students and warden.
Constraints:
1 < N = 10^3
Input Explanation:
The first line will take integer input N, indicating the number of students
The second line will have N spaced integers indicating the student roll number
Output Explanation:
if the N is even, then the interleave queue of students.
if the N is odd, then -1.
Input: Output:
4 1324
1234
Question 8:
Problem Statement:
A Professor explained the concept of doubly linked list to his class and wants to test their skills.
He has asked his students to write a logic in such a way so that, whenever a new integer value is
added to the list it should be placed in the right order along with a pointer for its forward and
backward nodes.
Your job is to make sure that the values provided from the console should be added to the list in
the ascending order without fail.
Input Explanation:
The first line will take integer input N, indicating the number if nodes in the linked list
The second line will have N spaced integers indicating the list node values to be inserted on the
right order
Output Explanation:
list in sorted order.
Input:
5
54321
Output:
12345
Question 9:
Problem Statement:
You are provided with the pointer to the head node of a linked list which is presorted, and the
data in the nodes is in ascending order. But there is a change of duplicate values on the linked list
node.
Your job is to delete nodes with duplicate values in the list and return a sorted list with distinct
values in the original list.
There may be a change that the given head pointer may be null indicating that the list is empty.
Constraints:
1 < N ≤ 10^3
Input Explanation:
The first line will take integer input N, indicating the number if nodes in the linked list
The second line will have N spaced integers indicating the sorted list node values
Output Explanation:
distinct values from list in sorted order.
Input:
7
1223345
Output:1 2 3 4 5
Question 10:
Problem Statement:
There are N persons standing in a payment desk queue, numbered 1 to N from left to right. For
each valid i, the i-th persons has a height hi
Two persons i and j can see each other if there are no taller persons standing between them.
Formally, persons i and j (i< j) can see each other if for each integer k (i<k<j), hk<= hi ,hj
For each valid i, determine the number ci of persons that have the same height as persons i and
can be seen by persons i.
Input Explanation:
The first line of each test case contains a single integer N.
The second line contains N space-separated integers h1, h2, … ,hN
Output Explanation:
For each test case, print a single line containing N space-separated integers c1, c2, … ,cN
Input:
3
121
Output:
000
Question 11:
Problem Statement:
Riya is enjoying a carnival with his friends and they come across a stall which offers them to
play a simple game and win some soft toys.
The game is about a numbered token stack, where the stall owner will ask the player to provide a
number of her choice N, then stall owner randomly pick those N number of tokens from a box
and passes them through a machine which puts those numbered tokens randomly in a stack.
Now if N is odd then the element inserted to the top of stack is discarded and then the
consecutive pairs are being checked.
Constraints:
1 < N ≤ 10^3
-32000 ≤ token number < 32000
Input Explanation:
The first line contains a single integer ‘N’ denoting the number of tokens to be fed to the
machine.
The second line contains the N number of space separated integers .
Output Explanation:
‘yes’ if the consecutive pairs of numbers were found on the provided stack else ‘no’
Input:
9
4 5 -2 -3 11 10 5 6 20
Output:
yes
Question 12:
Problem Statement:
Vinay is a hard working student. He intensively prepares for the exams by completing all the
exercises provided in his book! Before exams he rigorously practices book exercises. Also, He
writes down and keeps updating the remaining number of exercises on the Last Page of each
book.
Vinay has a lot of books messed up on the almirah. Therefore, he wants to pile up the books that
still have some remaining exercises into a single pile. He will grab the books one-by-one and add
the books that still have remaining exercises to the top of the pile.
Whenever he wants to do a book exercise, he will pick the book with the minimum number of
remaining exercises from the pile. In order to pick the book, he has to remove all the books
above it. Therefore, if there are more than one books with the minimum number of remaining
exercises, he will take the one which requires the least number of books to remove. The removed
books are returned to the messy almirah. After he picks the book, he will do all the remaining
exercises and trash the book.
Since the number of books is rather large, he needs your help to tell him the number of books he
must remove, for picking the book with the minimum number of exercises.
Note: that more than one book can have the same name.
Constraints:
1 < N ≤ 1,000,000
0 ≤ (the count of remaining exercises of each book) < 100,000
1≤ name of book(small letters) ≤ 15
Whenever he wants to do a book exercise, there is at least one book in the pile.
Input Explanation:
The first line contains a single integer ‘N’ denoting the number of actions.
Then ‘N’ lines follow. Each line starts with an integer.
If the integer is -1, that means Harry wants to do a book exercise. Otherwise, the integer is the
number of the remaining exercises in the book he grabs next.
This is followed by a string denoting the name of the book.
Output Explanation:
For each -1 in the input, output a single line containing the number of books Harry must remove,
followed by the name of the book that Harry must pick.
Input:
6
9 cplusplus
6 datastruct
8 dbms
-1
3 ml
-1
Output:
1 datastruct
0 ml
Question 13:
Problem Statement:
Anup just explained the concept of operator overloading to his students and wanted them to test
their knowledge.
He wants you to design a class with at least one variable(to store text phrases with or without
space) that should be accessible inside class in such a way that its two or more objects should
allow the concatenation(using + operator) of its variable containing a text value.
Input
For Each test case the first line will provide the first text phrase.
Next line will provide the second text phrase.
Output
In one line it should display concatenated text phrases separated by single space.
Input:
Anup
Test1
Output:Anup Test1
Constraints:The size of phrases passed to the class object will not exceed 100 characters.
Question 14:
Problem Statement:
You are given the head of a linked list, and an integer k.
Return the head of the linked list after swapping the values of the kth node from the beginning
and
the kth node from the end (the list is 1-indexed).
Input:
5
12345
2
Output:1 4 3 2 5
Constraints:
The number of nodes in the list is n.
1 <= k <= n <= 10^5
0 <= Node.val<= 100
Question 15:
Problem Statement:
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding
the number itself. Write a program in C++ to print all perfect numbers between a given range
using the function.
Input:
Line 1: Starting range number
Line 2: Ending range number
Output:
All the numbers are separated by a space in ascending order.
Input:
1
20
Output:6
Question 16:
Problem Statement:
An anagram is a word formed by rearranging the letters of a word, typically using all the original
letters exactly once. For example, the word binary into brainy and the word adobe into the abode.
Write a program in C++ to check whether two given words are an anagram. If both the words are
anagram, print TRUE else FALSE.
Input:
MyAnatomy
AnatomyMy
Output:
TRUE
Question 17:
Problem Statement:
Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n).
For example:
Number of terms: 3
1=1
1+2=3
1+2+3=6
sum of series = 10
In first line, you enter the number of terms, second line will print the sum.
Input: 3
Output: 10
Question 18:
Problem Statement:
Write a program in C++ to calculate the series (1) + (2+2) + (3+3+3) + (4+4+4+4) + ... +
(n+n+n+n+...+n).
Example:
Number of terms: 3
1=1
2+2 = 4
3+3+3 = 9
The sum of the series is: 14
In the first line, you enter the number of terms, second line will print the sum.
Input: 3
Output: 14
Question 19:
Problem Statement:
Write a C++ program to which reads n digits chosen from 0 to 9 and counts the number of
combinations where the sum of the digits equals to given number. Do not use the same digits in a
combination.
For example, the combinations where n = 2 and s = 5 are as follows:
0+5=5
1+4=5
3+2=5
Input: 3 8 [ no of digits, SUM]
Output:5(Number of combination)
Question 20:
Problem Statement:
Write a C++ program to compute the sum of the specified number of Prime numbers.
For example, when n = 7,
s = 2 + 3 + 5 + 7 + 11 + 13 + 17 = 58.
Input: 3
Output: 10