Questions For Hackerrank
Questions For Hackerrank
Engineering College
An Autonomous Institution
1Q) Write a program to input hexadecimal number from user and convert it into the
Decimal number.
Example
Input
Input hexadecimal: 1A
Output
Decimal number: 26
2Q) Given an integer array Arr of size N the task is to find the count of elements whose
value is greater than all of its prior elements.
Note : 1st element of the array should be considered in the count of the result.
For example,
Arr[]={7,4,8,2,9}
8 and 9 are also the elements that are greater than all of its previous elements.
Since total of 3 elements is present in the array that meets the condition.
Example 1:
Input
Output :
Example 2:
Output :
Constraints
● 1<=N<=20
● 1<=Arr[i]<=10000
3Q) A party has been organised on cruise. The party is organised for a limited time(T).
The number of guests entering (E[i]) and leaving (L[i]) the party at every hour is
represented as elements of the array. The task is to find the maximum number of guests
present on the cruise at any given instance within T hours.
Example 1:
Input :
● 5 -> Value of T
● [7,0,5,1,3] -> E[], Element of E[0] to E[N-1], where input each element is separated
by new line
● [1,2,1,3,4] -> L[], Element of L[0] to L[N-1], while input each element is separate
by new line.
Output :
Explanation:
● 1st hour:
Entry : 7 Exit: 1
No. of guests on ship : 6
2nd hour :
Entry : 0 Exit : 2
Hour 3:
Entry: 5 Exit: 1
Hour 4:
Entry : 1 Exit : 3
Hour 5:
Entry : 3 Exit: 4
Example 2:
Input:
4 -> Value of T
[3,5,2,0] -> E[], Element of E[0] to E[N-1], where input each element is separated by new
line.
[0,2,4,4] -> L[], Element of L[0] to L[N-1], while input each element in separated by new
line
Output:
Cruise at an instance
4Q) At a fun fair, a street vendor is selling different colours of balloons. He sells N
number of different colours of balloons (B[]). The task is to find the colour (odd) of the
balloon which is present odd number of times in the bunch of balloons.
Note: If there is more than one colour which is odd in number, then the first colour in
the array which is present odd number of times is displayed. The colours of the balloons
can all be either upper case or lower case in the array. If all the inputs are even in
number, display the message “All are even”.
Example 1:
● 7 -> Value of N
● [r,g,b,b,g,y,y] -> B[] Elements B[0] to B[N-1], where each input element is sepārated
by ṉew line.
Output :
● r -> [r,g,b,b,g,y,y] -> “r” colour balloon is present odd number of times in the bunch.
Explanation:
● r: 1 balloon
● g: 2 balloons
● b: 2 balloons
● y : 2 balloons
Example 2:
Input:
● 10 -> Value of N
● [a,b,b,b,c,c,c,a,f,c] -> B[], elements B[0] to B[N-1] where input each element is
separated by new line.
Output :
b-> ‘b’ colour balloon is present odd number of times in the bunch.
Explanation:
● a: 2 balloons
● b: 3 balloons
● c: 4 balloons
● f: 1 balloons
Here, both ‘b’ and ‘f’ have odd number of balloons. But ‘b’ colour balloon occurs first.
● Second Input : Accept N number of character values (B[]), where each value is
separated by a new line.
The output should be a single literal (Check the output in example 1 and example 2)
Constraints:
● 3<=N<=50
● B[i]={{a-z} or {A-Z}}
5Q) There is a JAR full of candies for sale at a mall counter. JAR has the capacity N,
that is JAR can contain maximum N candies when JAR is full. At any point of time.
JAR can have M number of Candies where M<=N. Candies are served to the customers.
JAR is never remain empty as when last k candies are left. JAR if refilled with new
candies in such a way that JAR get full.
Write a code to implement above scenario. Display JAR at counter with available
number of candies. Input should be the number of candies one customer can order at
point of time. Update the JAR after each purchase and display JAR at Counter.
Output should give number of Candies sold and updated number of Candies in JAR.
Given,
K =< 5, where k is number of minimum candies that must be inside JAR ever.
● Input Value
o 3
● Output Value
● Input Value
o 0
● Output Value
o INVALID INPUT
Display the most fit trainee (or trainees) and the highest average oxygen level.
Note:
● The oxygen value entered should not be accepted if it is not in the range between
1 and 100.
● If the calculated maximum average oxygen value of trainees is below 70 then declare
the trainees as unfit with meaningful message as “All trainees are unfit.
Example 1:
● INPUT VALUES
95
92
95
92
90
92
90
92
90
● OUTPUT VALUES
o Trainee Number : 1
o Trainee Number : 3
Note:
Round 1
Round 2
Round 3
Output must be in given format as in above example. For any wrong input final output should
display “INVALID INPUT”
7Q)Program to find if the given numbers are Friendly pair or not (Amicable or not) is
discussed here. Friendly Pair are two or more numbers with a common abundance.
● The first integer corresponds to number 1 and the second integer corresponds to
number 2.
Input format:
● If the given number is Harshad Number display Harshad Number or display Not
Harshad Number.
Sample Input:
1729
Sample Output:
Harshad Number
For example, 5 is an automorphic number, 5*5 =25. The last digit is 5 which same as the
given number. It has only a positive single digit number. If the number is not valid, it should
display "Invalid input".
If it is an automorphic number display Automorphic Number else display Not Automorphic
Number.
Input & Output format:
Input consist of an integer.
Sample Input :
7
Sample Output :
Not Automorphic Number
It was Raj's first day at school. His teacher Anu asked the students to meet every other
student in the class and to introduce about themselves. The teacher asked them to do
handshakes when they meet each other.
If there are n number of students in the class then find the total number of handshakes made
by the students.
Sample Input:
15
Sample output:
105
Algorithm to find the maximum number of handshakes
DESCRIPTION
It was Raj's first day at school. His teacher Anu asked the students to meet every other
student in the class and to introduce about themselves. The teacher asked them to do
handshakes when they meet each other.
If there are n number of students in the class then find the total number of handshakes made
by the students.
Sample Input:
15
Sample output:
105
12Q)Program to find all possible permutations in which 'n' people can occupy 'r' seats in a
theater is discussed here.
N friends are planning to go to a movie. One among them suggested few movies and all
others started to discuss and finally they selected a movie. One among them quickly booked
their tickets online, to their surprise they are unable to select their seats. All of them got
confused. Then anyhow, decided to go to the movie. They rushed to reach the theater on time.
Again they are surprised that no one was there in the theater. They are the only people about
to watch the movie. There is 'r' number of seats in which, 'n' number persons should sit. In
how many ways they can sit inside the theater?
Given the number of people 'n' and the number of seats 'r' as input. The task is to find the
different number of ways in which 'n' number of people can be seated in those 'r' number of
seats.
For example,
Input:
Number of people: 5
Number of Rows: 3
Output:
The total number of ways in which 'n' people can be seated in 'r' seats = 60.
Calculation:
P(n,r) =P(5,3)
=5! /(5?3)! = 5! / ( 5 ? 3 )!
= 120 / 2 = 60
Algorithm to find all possible permutations in which 'n' people can occupy 'r' seats in a theater
● Input the number of people 'n' and the number of seats 'r' from the user.
13Q)Program to find the number of times digit 3 occurs in each and every number from 0 to
n is discussed here. Given a number n as input, count the number of 3s occurring in all the
numbers from 0 to n.
For example,
Input: 100
Output: 20
Total number of 3s that appear from numbers 0 to 100 are {3, 13, 23, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 43, 53, 63, 73, 83, 93}
Algorithm to find the number of times digit 3 occurs in each and every number
2. Initialize count = 0.
4. Find if num % 10 == 3
5. Increment count
7. Return count.
14Q)Program to find all triplets with the given sum in the given array is discussed here.
Given an array of integers and a sum value, we need to iterate through the array and find all
possible triplets that sum to the given value
For example,
Consider the array: arr[] = {0, -1, 2, -3, 1}. The given sum is -2. In the given array, the triplets
with sum = -2 are {0, -3, 1} and {-1, 2, -3}.
Method 1:
● Use three loops and check one by one that sum of three elements is equal to the given
sum or not.
● If the sum of 3 elements is equal to the given sum, then print elements otherwise print
not found.
15Q)Program to find the saddle point coordinates in a given matrix is discussed here.A
saddle point is an element of the matrix, which is the minimum element in its row and
the maximum in its column.
123
Mat[3][3] = 4 5 6
789
Here, 7 is the saddle point because it is the minimum element in its row and maximum
element in its column.
16Q)Program to find if the given matrix is upper triangular or not is discussed here. A
square matrix is obtained as input from the user and it is checked if it is upper
triangular or not. A square matrix is said to be an upper triangular matrix if all the
entries below the diagonal are zero.
17Q)Program to find the sum of elements in each row and each column of the given
matrix and print the greatest of the same is discussed here. Input the matrix and find
the sum of each row and each column and display the row having the greatest sum and
also the column having the greatest sum.
Sum of row 1 = 1 + 2 + 3 = 6
Sum of row 2 = 4 + 5 + 6 = 15
Sum of row 3 = 7 + 8 + 9 = 24
Sum of column 1 = 1 + 4 + 7 = 12
Sum of column 2 = 2 + 5 + 8 = 15
Sum of column 3 = 3 + 6 + 9 = 18
18Q)Program to print the sum of elements in the Zigzag sequence in a given matrix is
discussed here. Given a matrix, the sum of elements in the zigzag sequence is displayed
as output.
19Q)Program to print the elements of a 2d array in the form of a matrix in spiral form is
discussed here. The below images gives you an idea of spiral order matrix printing.
Test case:
123456
7 8 9 10 11 12
13 14 15 16 17 18
Output:
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
A character variable holds the ASCII value (an integer number between 0 and 127)
rather than that character itself. That value is known as the ASCII value.
Input: S
Output: 83
Input: s
Output: 115
Input 1 :
Output 1:
HelloAll.WelcometoFace
Input 2:
Face
Output 2:
Face
22Q)Program to capitalize first and last letter of each word in a line is discussed here.
The basic algorithm is to keep track of the spaces and capitalize the letter before the
space and after space. The first letter and the last letter of the given line should be
capitalized.
● There may be two words like "me" where both letters must be capitalized.
Input: FacePrep
Output: fACEpREP
Input: FocucAcadeMy
Output: fOCUSaCADEmy
24Q)Program to find the number of days in a given month of a given year is discussed
here. The month and year are given as input, the number of days in that month is
displayed as output.
Sample Input:
1996
Sample Output:
Number of days is 31
25Q)Program to find if an array is a subset of another array is discussed here. If all the
elements of array 2 are found in array 1, then array 2 is said to be a subset of array
1.(Asked in recruitment drive of Wipro.)
For example,
arr1 = {1,2,3,4,5}
arr2 = {3,4,5}
arr3 = {1,2,3,4,5}
arr4 = {1,2,9}
26Q)Program to check if two strings are anagrams or not is discussed here. Two strings
are given as input and those strings have to be checked if they are anagrams or not.
Anagram means that both strings contain the same character set, only their order is
different. Therefore, in both strings, the frequency of each letter must be the same. For
example, strings "act" and "cat" are anagrams.
Method 1: Count the frequency of alphabets in both the strings and store them in respective
arrays. If the two arrays are equal, return true. Else, return false.
Method 2: Sort both the strings and compare if both the sorted strings are equal. If they are
equal, return true. Else, return false
This program will generate a digital clock using c program. The logic behind to
implement this program,
● Increase minute and check if it is equal to 60 then increase hour and reset minute to 0.