0% found this document useful (0 votes)
14 views

Coding Problems Set 1

Uploaded by

imakhil.1684
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Coding Problems Set 1

Uploaded by

imakhil.1684
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter 0

Coding/Programming
Problems for
Campus Placements
The book in your hand is the only textbook that gives you exposure regarding the kind
of problem asked and the format in which they are asked during the placement process
by the majority of the IT companies. You are required to strictly adhere to the
instructions otherwise your submission of the solution can be rejected.

The problems listed here, are just indicative, and will be expanded further in future
editions. The problems listed and similar had been asked in the previous placements in
the coding/programming section of the first phase (screening) by the majority of the IT
companies including TCS, Wipro, IBM, Microsoft, Infosys, Cognizant, Accenture,
Amazon, Facebook, etc. While going through the content of the book, try to solve these
problems, and this practice will help you to harness your problem-solving and coding
skills to crack the placement with flying colors.

As a special gesture, the readers of this book will be provided with placement
assistance free of cost, for which you may be required to register online.

Best wishes for your great future.

1. Area of Circle Output Format:


Write a program to find area of circle. Output consist of area of circle in mtsq correct
Input Format: to 2 decimal places.
Input consists of float value which Sample Input:
corresponds to radius of circle. 5.5
Sample Output:
2 Programming for Problem Solving with Python

94.99 Sample Output 2:


29/02/2019 invalid date.
2. Leap Year
A year will be a leap year if it is divisible by 4 4. Find Your Day of Birth
but not by 100. If a year is divisible by 4 and Write a program to find the day on which you
by 100, it is not a leap year unless it is also were born. Your date-of-birth is given in the
divisible by 400. format dd/mm/yyyy.
Write a program to check whether a given Input Format:
year is a leap year or not. Input consists of date in given format.
Input Format: Output Format:
Input consists of a single integer. Output consists of a single line. Refer sample
Output Format: output for details.
Output consists of a single line. Refer sample Sample Input 1:
output for details. Enter your date of birth dd/mm/yyyy:
Sample Input 1: 28/10/1955
2020 Sample Output 1:
Sample Output 1: You was born on Friday.
2020 is a leap year *Bill Gate, Founder of Microsoft, was born on this
Sample Input 2: day.
2019
Sample Output 2: 5. Armstrong Number
2019 is not a leap year
An Armstrong number is a 3-digit integer
such that the sum of the cubes of its digits is
3. Validity of Date equal to the number itself.
Write a program to check whether the given For example, 371 is an Armstrong number,
date in format dd/mm/yyyy is valid or not. since
Input Format: 3^3 + 7^3 + 1^3 = 371
Input consists of date in given format. Write a program to find whether a given 3-
Output Format: digit number is an Armstrong number or not.
Output consists of a single line. Refer sample Restrict your input to 3-didit number
output for details. otherwise display message “Wrong input”.

Sample Input 1: Input Format:


Enter date in format dd/mm/yyyy: Input consists of a single integer.
19/11/2020
Sample Output 1: Output Format:
19/11/2020 is valid date. Refer sample output for details.

Sample Input 1:
Sample Input 2: 153
Enter date in format dd/mm/yyyy: Sample Output 1:
29/02/2019 Armstrong Number
Chapter 0: Coding/Programming Problems for Campus Placements 3

Sample Input 2:
8. Factorial
135 Write a program to find the factorial of a
Sample Output 2: given natural number ‘n’.
Not an Armstrong Number
Input format:
Input consists of an integer value.
6. Prime Number
Output Format:
A prime number is a natural number that is Refer sample output for details.
divisible by only 1 and itself only.
Write a program to find whether the given Sample Input and Output 1:
natural number ‘n’ is a prime number or not. Enter given natural number: 4
4! = 24
Input Format:
Input consists of a single integer. Sample Input and Output 2:
Enter given natural number: 6
Output Format: 6! = 720
Refer sample output for details.
Sample Input and Output 1:
9. Palindrome Number
Enter the given natural number: 17 A Palindrome number is one which reads
15 is a prime number same from both the ends. For example, 1881,
232454.
Sample Input and Output 2:
Enter the given natural number: 145 Write a program to find whether the given
145 is not a prime number number ‘n’ is a palindrome or not.

7. Input Format:
Input consists of a single integer.
Sum of digits
Write a program to find the sum of digits of a Output Format:
given natural number ‘n’. Refer sample output for details.

Input format: Sample Input and Output 1:


Input consists of an integer value. Enter the given natural number: 1001
1001 is a palindrome.
Output Format:
Refer sample output for details. Sample Input and Output 2:
Enter the given natural number: 1100
Sample Input and Output 1: 1100 is not a palindrome.
Enter given natural number: 1345
Sum of digits in 1345 is 13.
10. Fibonacci Sequence
Sample Input and Output 2: Write a program to print first ‘n’ terns of the
Enter given natural number: 1111111111 Fibonacci sequence.
Sum of digits in 1111111111 is 10.
For example, first 7 terms of Fibonacci
sequence
4 Programming for Problem Solving with Python

0 1 1 2 3 5 8 Input consists of a single integer.


Input format:
Output Format:
Input consists of an integer value.
Refer sample output for details.
Output Format:
Sample Input and Output 1:
Refer sample output for details.
Enter the given number: 145
145 is a strong number.
Sample Input and Output:
Enter value for n: 10
Sample Input and Output 2:
First 10 terms of Fibonacci sequence are
Enter the given number: 110
0 1 1 2 3 5 8 13 21 34
110 is not a strong number.

11. Perfect Number 13. Special Number


A perfect number is a number in which the A 2-digit number is said to be a special
sum of its proper divisors is equal to the number if the sum of the sum of its digits and
number itself. Proper divisors of a number are the products of its digits is equal to the
all divisors of a number excluding itself. number itself.
Write a program to find whether a given
number ‘n’ is a perfect number or not. For example, 19 is a special number. The
digits in 19 are 1 and 9. The sum of the digits
Input format: is 10 and the product of the digits is 9, sum of
Input consists of an integer value. sum of digits and product of digits is 19.

Output Format: Write a program to find all special numbers.


Refer sample output for details.
Input Format:
Sample Input and Output 1: Nil
Enter given natural number: 6
6 is a perfect number. Output Format:
Sample Input and Output 2: List of all special numbers separated by one or
Enter given natural number: 12 more spaces. Refer to sample output for more
12 is not a perfect number. details.
Output:
Here is the list of special numbers.
12. Strong Number 19 29 39 49 59 69 79 89 99
A strong number is a number whose sum of
the factorial of its each digit equals to the
number itself. 14. GCD of numbers
Write a program to find the greater common
For example, 145 is a strong number since divisor (GCD)/highest common factor (HCF)
of two natural numbers ‘m’ and ‘n’.
1! + 4! + 5! = 1 + 24 + 120 = 145
Write a program to find whether a given Input Format:
number ‘n’ is a strong number or not. Input consists of 2 natural numbers.

Input Format:
Chapter 0: Coding/Programming Problems for Campus Placements 5

Output Format:
Refer sample input and output for formatting Sample Input:
specifications. 12
2
Sample Input and Output: 3
Enter first natural number ‘m’ : 36 Sample Output:
Enter second natural number ‘n’ : 27 1 1 2 3 4 9 8 27 16 81 32 243

GCD(36,27) = 9 17. Series Problem #2


Consider the following series:
15. LCM of numbers 1, 1, 2, 5, 4, 9, 8, 13, 16, 17, . . .
Write a program to find the least common
If you note carefully, you will find that odd
multiplier (LCM) of two natural numbers ‘m’
term represent GP with common ratio of 2,
and ‘n’.
and even term represent AP with a common
Input Format: difference of 4.
Input consists of 2 natural numbers.
Write a program to generate first ‘n’ terms of
this series when ‘r’ as common ratio of GP,
Output Format:
and ‘d’ is the common difference of AP.
Refer sample input and output for formatting
specifications. Input Format:
Input consists of three integers, n, r and d, in
Sample Input and Output: order, on separate lines.
Enter first natural number ‘m’ : 40
Enter second natural number ‘n’ : 130 Output Format:
Display the first ‘n’ terms of the series.
LCM(35,120) = 260 Sample Input:
12
2
16. Series Problem #1 4
Consider the following series: Sample Output:
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, . . . 1 1 2 5 4 9 8 13 16 17 32 21
If you note carefully, you will find that even
term represent one GP and odd term
represent second GP.

Write a program to generate first ‘n’ terms of


this series when common ratios of these two
GPs are given as ‘r1’ and ‘r2’, respectively.

Input Format:
Input consists of three integers, ‘n’, ‘r1’ and
‘r2’, in order, on separate lines.

Output Format:
Display the first ‘n’ terms of the series.
6 Programming for Problem Solving with Python

Sample Output:
18. Series Problem #3 20, 1, 16, 1, 12, 2, 8, 3, 4, 5.
Consider the following series:
1, 1, 1, 5, 2, 9, 3, 13, 5, 17, 8, 21, 13,. . . 20. Decimal to Binary
If you note carefully, you will find that odd Conversion
term represent terms of Fibonacci sequence, Write a program to perform decimal to binary
and even term represent AP with a common conversion.
difference of 4.
Input Format:
Write a program to generate first ‘n’ terms of Input consists of a single decimal number.
this series with ‘d’ as the common difference
of AP. Output Format:
Input Format: Print the equivalent binary number.
Input consists of two integers, ‘n’ and ‘d’ on
Sample Input and Output:
separate lines.
Enter the decimal number: 10
Output Format: Binary equivalent of decimal number 10 is
Display the first ‘n’ terms of the series. 1010.

Sample Input:
10 21. Binary to decimal
4 Conversion
Sample Output:
Write a program to perform binary to decimal
1 1 1 5 2 9 3 13 5 17
conversion.

19. Series Problem #4 Input Format:


Input consists of a single binary number.
Consider the following series:
Output Format:
20, 1, 16, 1, 12, 2, 8, 3, 4, 5, 0, 8, -4, 13, -8,. . .
Print the equivalent decimal number.
If you note carefully, you will find that the
odd terms represent AP with a common Sample Input and Output:
difference of -4, and even terms represent Enter the binary number: 1010
term of Fibonacci sequence. Decimal equivalent of binary number 1010 is
10.
Write a program to generate first ‘n’ terms of
this series with ‘d’ as the common difference
of AP.
22. Sorting
Write a program to sort elements of an array
Input Format: named ‘a’ of size ‘n’. Elements of array are of
Input consists of two integers, ‘n’ and ‘d’ on type ‘int’.
separate lines.
Input Format:
Output Format:
Input consists of ‘n+1’ integers. The first
Display the first ‘n’ terms of the series.
integer corresponds to ‘n’. The next ‘n’
Sample Input: integers correspond to the elements in the
10 array.
-4
Output Format:
Chapter 0: Coding/Programming Problems for Campus Placements 7

Display the elements of the sorted array. The first line of the input consists of an integer
‘n’ that corresponds to the number of
Sample Input: elements in the input array.
5 The second line consists of elements of array
3 separated by space.
2
6 Output Format:
8 List of numbers that are leaders, one single
1 line, separated by space.
Sample Output:
1 2 3 6 8 Sample Input 1:
6
16 17 4 3 5 2
23. First 3-Largest Numbers Sample Output 1:
Write a program to find the first 3-largest 17 5 2
numbers from an array named ‘a’ of size ‘n’,
without sorting the array. The elements of the Sample Input 2:
array are of type ‘int’. 6
16 10 4 3 18 19
Input Format: Sample Output 2:
Input consists of ‘n+1’ integers. The first 19
integer corresponds to ‘n’. The next ‘n’
integers correspond to the elements in the 25. Majority Element
array.
A majority element in an array of size ‘n’ is an
element that appears more than ‘n/2’ times.
Output Format:
Display the first 3-largest numbers in order. 1. If there exists a majority element, then
Sample Input: display that element.
10 2. If such element doesn’t exist, then display
35 20 61 80 19 40 100 75 57 60 “No Majority Element exists”.
Sample Output:
100 Write a program that prints the majority
80 element in an array.
75 Input Format:
The first line of the input consists of an integer
‘n’ that corresponds to the number of
24. Leaders elements in the input array.
An element is a leader if it is greater than all The second line consists of elements of array
the elements to its right side, i.e., following it. separated by space.
And the rightmost element is always a leader.
Output Format:
For example in the array {16, 17, 4, 3, 5, 2}, Refer sample output formatting specifications.
leaders are 17, 5 and 2.
Sample Input 1:
Write a program to print all the leaders in the
9
array.
334244244
Sample Output 1:
Input Format: 4
8 Programming for Problem Solving with Python

Sample Input 2: Sample Input 1:


8 8
334254246
Sample Output 2: Let us assume that following are the elements
No Majority Element exists. generated randomly:
31532126
26. Sorting a String
Write a program to sort a string in Sample Output 1:
alphabetical (dictionary) order. 312
Sample Input 2:
Input Format:
5
Input consists of a string. Assume that the
input string consists of only letters and the Let us assume that following are the elements
maximum length of the string is 50. generated randomly:

Output Format: 31542


A string whose letters are sorted in Sample Output 2:
alphabetical order. No duplicates

Sample Input:
Anitha 28. Distinct Elements
Given an array of ‘n<=50’ elements which
Sample Output: contains elements from 1 to n, generated
Aahint randomly, with any of these numbers
appearing any number of times.

27. Duplicate Elements Write a program to find these repeating


Given an array of ‘n<=50’ elements which numbers.
contains elements from 1 to n, generated
Example:
randomly, with any of these numbers
Let A = {1, 2, 3, 1, 3, 6, 6, 5}
appearing any number of times.
Distinct elements = 2 5
Write a program to find these repeating
Input Format:
numbers.
A single number ‘n’ that corresponds to the
Example: number of elements in the array.
Let A = {1, 2, 3, 1, 3, 6, 6, 3}
Duplicate elements = 1 3 6 Output Format:
Refer sample output formatting specifications.
Input Format: Sample Input 1:
A single number ‘n’ that corresponds to the 8
number of elements in the array.
Let us assume that following are the elements
Output Format: generated randomly:
Refer sample output formatting specifications.
1, 2, 3, 1, 3, 6, 6, 5
Chapter 0: Coding/Programming Problems for Campus Placements 9

Sample Output 1: 45
25
Sample Input 2: 30. Missing Number
6 Write a program to find the missing integer
among the array of ‘n<=9’ integers that are in
Let us assume that following are the elements
the range 1 to (n+1).
generated randomly:
Input Format:
325325
The first line of the input consists of an integer
Sample Output 2: ‘n’ that corresponds to the number of
No distinct element. elements in the input array.

29. To Remove Duplicate The second line consists of ‘n’ integers in the
range 1 to (n+1), separated by space.
Elements
Given an array of ‘n<=50’ elements. Write a Output Format:
program to remove duplicate elements from List of missing number(s).
the array.
Sample Input 1:
Input Format: 5
The first line of the input consists of an integer 15326
‘n’ that corresponds to the number of
elements in the input array. Sample Output 1:
The next 'n' lines in the input correspond to 4
the elements in the array.
Output Format: 31. Sum
Refer sample output formatting specifications. Given an unsorted array ‘a’ of size ‘n’ of non-
Sample Input: negative integers, find a continuous sub-array
10 which adds to a given number sum.
10
22 Input Format:
31 The first line contains an integer ‘n’ denoting
10 the size of the array.
31 The second line contains integers denoting the
16 elements
26 of the array separated by space.
45 The last line contains an integer, denoting the
22 sum.
31
Output Format:
Sample Output 1: The output line contains integers denoting the
10 indexes of the sub-array.
22
31
16 Sample Input 1:
26 6
10 Programming for Problem Solving with Python

1 4 3 0 5 10 33. Reverse Sub-array


7
Given an array ‘a’ of size ‘n’ of non-negative
Sample Output 1: integers. Write a program to reverse every
Sum found between indexes 1 and 4 sub-array of size ‘k’, where ‘n’ is multiple of
‘k’.
Sample Input 2:
7 Input Format:
1 4 3 0 5 10 The first line contains an integer ‘n’ denoting
9 the size of the array.
Sample Output 2: The second line contains integers denoting the
No sub-array found. elements of the array separated by space.
The last line contains an integer ‘k’ denoting
the size of the sub-array.
32. Equilibrium
Equilibrium position in an array is a position Output Format:
such that the sum of elements before of it is The output line contains integers denoting the
equal to the sum of elements after it. elements of the array, with elements of each
sub-array reversed..
Given an array ‘a’ of size ‘n’ of non-negative
integers, find the index in the array where Sample Input:
equilibrium occurs first in the array. 6
-7 1 5 6 -4 2
Input Format:
The first line contains an integer ‘n’ denoting Sample Output:
the size of the array. 5 1 -7 2 -4 6
The second line contains integers denoting the
elements of the array separated by space.
34. ASCII to Integer
Output Format:
The output line contains integers denoting the Conversion
equilibrium position or -1 if no equilibrium Your task is to implement the function atoi.
exists. The function takes a string ‘str’ denoting a
decimal number string, converts it to integer
Sample Input 1: and returns it.
7
-7 1 5 2 -4 2 1 Input Format:
A numeric string.
Sample Output 1:
3 Output Format:
Integer value
Sample Input 2:
7 Sample Input:
1 4 3 0 5 10 “8671”

Sample Output 2: Sample Output:


-1 8671
Chapter 0: Coding/Programming Problems for Campus Placements 11

35. Triplets 37. Left Rotation


Given an array ‘a’ of size ‘n’. Write a program A left rotation operation on an array of
to find all the triplets such that sum of any size n shifts each of the array's elements 1 unit
two elements is equal to the third element. to the left. Given an integer, d, rotate the array
that many steps left. Example,
Input Format: d=2
The first line contains single integers ‘n’ arr = [1,2,3,4,5]
denoting the size of the array. After 2 rotations, arr’ = [3,4,5,1,2].
The next line contains the elements of the Input Format:
array, separated by space. The first line contains two space-separated
Output Format: integers that denote the number of integers,
The output line contains all those triplets and the number of left rotations to perform.
whose sum of any two elements is equal to The second line contains space-separated
the third element. integers that represent the elements of the
array.
Sample Input:
8 Output Format:
7 2 5 6 -4 6 3 9 8 List of elements of left rotated array.

Sample Output: Constraints


(7,2,5) (6,3,9)  1≤ n ≤ 105
 1≤ d ≤ n
36. Counting Bits  1≤ a[i] ≤ 106
Write a program that counts the bits that are 1 Sample Input:
in the binary representation of natural 54
number ‘n’. 12345

Input Format: Sample Output:


Input consists of a single natural number ‘n’. 51234

Constraint:
20 <= N <=1000
38. Magic Triplets
Given an array ‘a’ of ‘n’ integers, a triplet (a[i],
Output Format: a[j], a[k]) is called Magic Triplet if a[i] < a[j] <
Output consists of the counts of 1s in the a[k] and i < j < k. The task is to count number
binary representation of ‘n’. of magic triplets in a given array.
Sample Input 1: Input Format:
127 (01111111) The first line contains an integer ‘n’ denoting
the size of array.
Sample Output 1:
Number of 1-bits in 127 = 7 The second line contains ‘n’ space-separated
integers denoting the elements of the array.
Sample Input 2:
32 (100000) Output Format:
Single number representing the number of
Sample Output 2: triplets.
Number of 1-bits in 32 = 1
12 Programming for Problem Solving with Python

Constraints:
1 <= n <= 103 Constraints:
1 <= a[i] <= 5000 1. Modulus operation by 2 as well 10 is not
Sample Input 1: 2. Use of if statement is not permitted.
3
321 Output Format:
Sample Output 1: Refer sample output formatting specifications.
0
Sample Input 1:
Sample Input 2: Enter natural number : 35
4
Sample Output 1:
1234
35 is odd number.
Sample Output 2:
2 Sample Input 2:
Enter natural number : 120
Numbers with alternate bits .
Sample Output 2:
Input Format: 120 is even number.
Input consists of a single natural number ‘n’.
Output Format:
Refer sample output formatting specifications. Write a program to swap two natural
numbers.
Sample Input 1:
5 (101) Input Format:
Input consists of a two natural number a & b
Sample Output 1: separated by space.
Number has alternate pattern of 0s and 1s.
Constraints:
Sample Input 2: 1. Use of third variable is not allowed
15 (1111) 2. Only single statement is permitted to
Sample Output 2: facilitate the swapping.
Number doesn’t have alternate pattern of 0s 3. 0 < a, b ≤ 10^7
and 1s. Output Format:
Refer sample output formatting specifications.
N or ODD Sample Input:
Write a program to check whether the given Enter two natural number a & b : 20 25
natural number is EVEN or ODD.
Sample Output:
Input Format: After swapping
Input consists of a single natural number ‘n’. a = 25, b = 20

You might also like