Coding Problems Set 1
Coding Problems Set 1
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.
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:
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
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.
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:
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.
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
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