0% found this document useful (0 votes)
61 views17 pages

Questions For Hackerrank

The document outlines various programming tasks and challenges for students in a Computer Science and Engineering department. It includes problems related to number conversions, array manipulations, guest counting, balloon color frequency, candy jar management, fitness tests, friendly pairs, Harshad numbers, automorphic numbers, handshake calculations, permutations, counting digit occurrences, triplet sums, saddle points in matrices, and upper triangular matrices. Each problem specifies input formats, output requirements, and constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views17 pages

Questions For Hackerrank

The document outlines various programming tasks and challenges for students in a Computer Science and Engineering department. It includes problems related to number conversions, array manipulations, guest counting, balloon color frequency, candy jar management, fitness tests, friendly pairs, Harshad numbers, automorphic numbers, handshake calculations, permutations, counting digit occurrences, triplet sums, saddle points in matrices, and upper triangular matrices. Each problem specifies input formats, output requirements, and constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

ACE

Engineering College
An Autonomous Institution

Department of Computer Science and Engineering

Programs for hackerrank (Company-Based Questions)

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}

As 7 is the first element, it will consider in the result.

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.

Hence the output = 3.

Example 1:

Input

5 -> Value of N, represents size of Arr

7-> Value of Arr[0]

4 -> Value of Arr[1]

8-> Value of Arr[2]

2-> Value of Arr[3]


9-> Value of Arr[4]

Output :

Example 2:

5 -> Value of N, represents size of Arr

3 -> Value of Arr[0]

4 -> Value of Arr[1]

5 -> Value of Arr[2]

8 -> Value of Arr[3]

9 -> Value of Arr[4]

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 :

8 -> Maximum number of guests on cruise at an instance.

Explanation:

● 1st hour:

Entry : 7 Exit: 1
No. of guests on ship : 6

2nd hour :

Entry : 0 Exit : 2

No. of guests on ship : 6-2=4

Hour 3:

Entry: 5 Exit: 1

No. of guests on ship : 4+5-1=8

Hour 4:

Entry : 1 Exit : 3

No. of guests on ship : 8+1-3=6

Hour 5:

Entry : 3 Exit: 4

No. of guests on ship: 6+3-4=5

Hence, the maximum number of guests within 5 hours is 8.

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:

From the input array above:

● r: 1 balloon

● g: 2 balloons

● b: 2 balloons

● y : 2 balloons

Hence , r is only the balloon which is odd in number.

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:

From the input array above:

● 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.

Hence , b is the output.

Input Format for testing

The candidate has to write the code to accept: 2 input


● First input: Accept value for number of N(Positive integer number).

● Second Input : Accept N number of character values (B[]), where each value is
separated by a new line.

Output format for testing

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.

If Input is more than candies in JAR, return: “INVALID INPUT”

Given,

N=10, where N is NUMBER OF CANDIES AVAILABLE

K =< 5, where k is number of minimum candies that must be inside JAR ever.

Example 1:(N = 10, k =< 5)

● Input Value

o 3

● Output Value

o NUMBER OF CANDIES SOLD : 3

o NUMBER OF CANDIES AVAILABLE : 7

Example : (N=10, k<=5)

● Input Value

o 0

● Output Value

o INVALID INPUT

o NUMBER OF CANDIES LEFT : 10


6Q) Selection of MPCS exams include a fitness test which is conducted on ground.
There will be a batch of 3 trainees, appearing for running test in track for 3 rounds. You
need to record their oxygen level after every round. After trainee are finished with all
rounds, calculate for each trainee his average oxygen level over the 3 rounds and select
one with highest oxygen level as the most fit trainee. If more than one trainee attains the
same highest average level, they all need to be selected.

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.

● Average Oxygen Values should be rounded.

Example 1:

● INPUT VALUES

95

92

95

92

90

92

90

92

90

● OUTPUT VALUES

o Trainee Number : 1

o Trainee Number : 3

Note:

Input should be 9 integer values representing oxygen levels entered in order as

Round 1

● Oxygen value of trainee 1

● Oxygen value of trainee 2


● Oxygen value of trainee 3

Round 2

● Oxygen value of trainee 1

● Oxygen value of trainee 2

● Oxygen value of trainee 3

Round 3

● Oxygen value of trainee 1

● Oxygen value of trainee 2

● Oxygen value of trainee 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.

Input & Output format:

● Input consists of 2 integers.

● The first integer corresponds to number 1 and the second integer corresponds to
number 2.

● If it is a Friendly Pair display Friendly Pair or displays Not Friendly Pair.

For example,6 and 28 are Friendly Pair.

(Sum of divisors of 6)/6 = (Sum of divisors of 28)/28.

8Q)Program to check whether a number is a Harshad number or not (Niven number) is


discussed here. Harshad Number is an integer that is divisible by the sum of its digits.

Input format:

● Input consists of 1 integer.

● If the given number is Harshad Number display Harshad Number or display Not
Harshad Number.

Sample Input:
1729

Sample Output:

Harshad Number

9Q)Program to check whether a number is Automorphic number or not is discussed here.


An automorphic number is a number whose square ends with the number itself.

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

10Q)Program to find the maximum number of handshakes is discussed here. Given a


positive integer n, find out the total number of handshakes possible.
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.

Input format and output format:

● Input consists of 1 integer.

● First input corresponds to the total number of students.


● Output consists of 1 integer.

Sample Input:

15

Sample output:

105
Algorithm to find the maximum number of handshakes

● Input the number of people (n).

● Find nC2, calculated as n * (n-1) / 2.


● Print the calculated result.

11Q)Program to find the maximum number of handshakes is discussed here. Given a


positive integer n, find out the total number of handshakes possible.

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.

Input format and output format:

● Input consists of 1 integer.

● First input corresponds to the total number of students.

● Output consists of 1 integer.

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.

● Calculate Permutation, p(n,r).


● P(n,r) = n! / (n - r)!
● Display the result.

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

1. Input the number n from the user.

2. Initialize count = 0.

3. Repeat for all numbers from o to n.

4. Find if num % 10 == 3

5. Increment count

6. Divide the number by 10 and from step 4.

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.

For example, consider the matrix given below

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.

For example, consider the matrix

mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Sum of row 1 = 1 + 2 + 3 = 6
Sum of row 2 = 4 + 5 + 6 = 15

Sum of row 3 = 7 + 8 + 9 = 24

Row 3 is having the greatest sum.

Sum of column 1 = 1 + 4 + 7 = 12

Sum of column 2 = 2 + 5 + 8 = 15

Sum of column 3 = 3 + 6 + 9 = 18

Column 3 is having the greatest sum.

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:

Input: 3 6 //matrix size

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

20Q)Program to find the ASCII value of a character is discussed here. Given a


character as input, the ASCII value of the character is displayed as output.

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.

For example, consider the given character as input.

Input: S

Output: 83

Input: s

Output: 115

21Q)Program to remove spaces from a string is discussed here. Given a string


containing white spaces, the task is to remove all the spaces from the string and produce
it as output.
For example, consider the following string

Input 1 :

Hello All. Welcome to Face

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 are only a few things that need to be considered:

● More than one occurrence of spaces between two words.

● There can be a single word like 'a' that needs to be capitalized.

● There may be two words like "me" where both letters must be capitalized.

23Q)Program to toggle each character in a string is discussed here. A string is given as


input in which all the lower case letters are converted to upper case and the upper case
letters are converted to lower case.

For example, consider the following string

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}

arr2 is a subset of arr1.

arr3 = {1,2,3,4,5}

arr4 = {1,2,9}

arr4 is not a subset of arr3.

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.

The solution to this problem can be given in two different ways.

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

27Q)C program to print character without using format specifiers

28Q)C program to design a digital clock

This program will generate a digital clock using c program. The logic behind to
implement this program,

● Initialize hour, minute, seconds with 0.

● Run an infinite loop.


● Increase second and check if it is equal to 60 then increase minute and reset second to
0.

● Increase minute and check if it is equal to 60 then increase hour and reset minute to 0.

● Increase hour and check if it is equal to 24 then reset hour to 0.

29Q)Write a c program to delete the all consonants from given string.

30Q)Write a c program to swap two numbers without using third variable.

You might also like