[Link] Computer Science & Engineering / B.
Sc (H) CS
Problem Statements (Basic to String)
Sr. Simple logic, loops, condi onals
1 WAP to find a factorial of a given integer (itera ve and recursive)
2 WAP to find a summa on of a digit of a given number. (Itera ve and recursive)
3 WAP to find a total odd and total even digit of a given number.
4 WAP to find whether a number is Odd or Even without using a % operator.
5 WAP to find a prime number between range (range should be entered by user).
6 WAP to find weather given number is Armstrong number is not.
7 WAP to find Max, Min, Average of n numbers, n should be taken from user and all n value should
be taken from user (Note that you are not allowed to use an array for this)
8 WAP to calculate an angle between hour and minute hand. (Hours and minutes should be
taken from user)
9 WAP to find a power a^b (without using power and mul plica on opera on)
10 WAP to find a quo ent and reminder of 2 number (bigger number should be divided by lower
number) and you are not allowed to use a division and quo ent operator.
11 WAP to find weather given number is Ugly or not.
An ugly number is a positive integer which does not have a prime factor other than 2, 3, and 5.
Input: n = 6 Output: true Explana on: 6 = 2 × 3
12 WAP to find weather given number is Kaprekar or not.
A Kaprekar number is a non-negative integer that, when squared, can be split into two parts
whose sum equals the original number.
For E.g. 45 is a Kaprekar number because 45 squared (2025) can be split into 20 and 25, and 20 +
25 = 45.
13 WAP to find weather given number is Automorphic or not.
An automorphic number is a number whose square ends with the same digits as the number
itself.
For example, 5 is automorphic because 5² = 25, which ends in 5.
Similarly, 76 is automorphic because 76² = 5776, which ends in 76.
14 WAP to find weather given number is Pronic or not.
A Pronic Number is defined as a number that is the product of two consecutive non-negative
integers. In other words, N is a Pronic Number if there exists a non-negative integer k such that N
= k * (k + 1).
E.g. 6 is a Pronic Number because 6=2*3
15 WAP for following Scenario.
Given n rupees and a chocolate price of m for each chocolate, with a wrapper exchange offer of 1
chocolate per k wrappers, calculate the total number of chocolates you can eat with n rupees.
Number System & Bit Manipula on
16 WAP to convert a Decimal number to BCD.
17 WAP to convert a Binary to Decimal.
18 WAP to convert an Octal into hexa-decimal.
19 WAP to convert a hexa-decimal to Octal.
20 WAP to Convert a Decimal to Octal and Vice versa.
21 WAP to Convert a Decimal to Hexa-decimal and vice versa.
[Link] Computer Science & Engineering / [Link] (H) CS
22 Write a program to take 2 numbers from user and find out the distance between them. (How to
compute distance: If number is 10 and 18 then 10 in binary 1010 and 18 in binary is 10010 and
distance is 2 means total number of bits that needs to be changed when 10 is converted into 18
or 18 is converted into 10, do not convert the number into binary)
23 Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, ...]
Input: n = 11
Output: 0
Explana on: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of
the number 10.
Pa ern Prin ng
24 Print a following pa ern
1
12
123
1234
25 Print a following pa ern
*********
*******
*****
***
*
26 Print a following pa ern
*********
**** ****
*** ***
** **
* *
27 Write a program to print following pattern
For n=4
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
28 WAP to implement a following pattern
1
1*1
1*3*1
1*3*5*3*1
[Link] Computer Science & Engineering / [Link] (H) CS
29 Implement a following pattern
* * * * * * *
* *
* *
*
* *
* *
* * * * * * *
Arrays – Basics & Manipula on
30 WAP to find a sum of even number into 1D array.
31 WAP to enter an element at specific posi on into array. (Do not take a new array)
32 WAP to delete an element from array specified by user. if element is not found print a message
“Element is not found” (do not take a new array).
33 WAP to check weather number is present in array or not (using recursion only) and the
func on’s syntax is given below
Int isInArray(int a[],int m);
Where int a[] is Array of integer and m is element to be searched.
35 Given an array, rotate the array to the right by k steps, where k is non-nega ve.
Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explana on:
rotate 1 step to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
36 You are given an integer num. Rearrange the digits of num such that its value is minimized
and it does not contain any leading zeros.
Return the rearranged number with minimal value.
Note that the sign of the number does not change a er rearranging the digits.
Input: num = 310
Output: 103
Explana on: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301,
310.
The arrangement with the smallest value that does not contain any leading zeros is 103.
37 Given an array of N integers, and an integer K, find the number of pairs of elements in the
array whose sum is equal to K.
e.g. Input: N = 4, K = 6
arr[] = {1, 5, 7, 1}
Output: 2
Explana on:
arr[0] + arr[1] = 1 + 5 = 6
and arr[1] + arr[3] = 5 + 1 = 6.
38 Write a Func on that returns either 1 or 0 based on following condi on if the array is in
ascending order and occurrence of that number at least 3 then it should return 1 otherwise
it should return 0.
[Link] Computer Science & Engineering / [Link] (H) CS
e.g.
if A=[1,1,1,2,2] it should return 0
if A=[1,1,1,3,3,3,3] it should return 1
if A=[2,2,2,1,1,1] it should return 0
39 You are given a 0-indexed array nums consis ng of posi ve integers. You can choose two
indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of
nums[j].
Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices
i and j that sa sfy the condi ons.
Input: nums = [18,43,36,13,7]
Output: 54
Explana on: The pairs (i, j) that sa sfy the condi ons are:
- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
So the maximum sum that we can obtain is 54.
41 WAP to find a Mul plica on of 2 Matrix (dimension and value should be entered by user)
42 Given 2 sorted arrays a[] and b[], each of size n, the task is to find the median of the array
obtained after merging a[] and b[].
Input: a[] = [1, 12, 15, 26, 38], b[] = [2, 13, 17, 30, 45]
Output: 16
Explanation: The middle two elements are 15 and 17, so median = (15 + 17)/2 = 16
Sor ng Algorithms
43 WAP to sort an Array using Bubble sort.
44 WAP to sort an Array using inser on sort.
45 WAP to sort an Array using Selec on sort.
46 WAP to sort an Array using Bucket sort.
47 WAP to sort an Array using Radix sort.
48 WAP to sort an Array using Coun ng sort
49 WAP to sort an Array using Tree sort.
50 WAP to sort an Array using shell sort.
51 WAP to sort an Array using Quick sort.
52 WAP to sort an Array using Heap sort.
53 WAP to sort an Array using Merge sort.
Strings & Parsing
54 WAP to find whether string is palindrome or not.
55 Find a next lexicographical order string from a given string, if input is abc output is acb (2
condi on must be followed 1st the string must be greater than the inpu ed string and the
outpu ed string must be the smallest string from all possible string.
Suppose there another string possible from this is acb, bac, bca, etc., but smallest is acb )
56 You are given a 0-indexed binary string s which represents the types of buildings along a
street where:
s[i] = '0' denotes that the ith building is an office and
s[i] = '1' denotes that the ith building is a restaurant.
As a city official, you would like to select 3 buildings for random inspec on. However, to
ensure variety, no two consecu ve buildings out of the selected buildings can be of the same
type.
[Link] Computer Science & Engineering / [Link] (H) CS
For example, given s = "001101", we cannot select the 1st, 3rd, and 5th buildings as that
would form "011" which is not allowed due to having two consecu ve buildings of the same
type.
Return the number of valid ways to select 3 buildings.
Input: s = "001101"
Output: 6
Explana on:
The following sets of indices selected are valid:
- [0,2,4] from "001101" forms "010"
- [0,3,4] from "001101" forms "010"
- [1,2,4] from "001101" forms "010"
- [1,3,4] from "001101" forms "010"
- [2,4,5] from "001101" forms "101"
- [3,4,5] from "001101" forms "101" No other selec on is valid. Thus, there are 6 total ways
57 Write a program that recognize strings that contains a pa ern wcwR (where w={a,b} and wR
means reverse of string w)
58 Given an array of strings strs, group the anagrams together. You can return the answer in
any order.
Input: strs = ["eat","tea","tan","ate","nat","bat"] Output:
[["bat"],["nat","tan"],["ate","eat","tea"]]
59 A valid number can be split up into these components (in order):
A decimal number or an integer.
(Op onal) An 'e' or 'E', followed by an integer.
A decimal number can be split up into these components (in order):
(Op onal) A sign character (either '+' or '-').
One of the following formats:
One or more digits, followed by a dot '.'.
One or more digits, followed by a dot '.', followed by one or more digits.
A dot '.', followed by one or more digits.
An integer can be split up into these components (in order):
(Op onal) A sign character (either '+' or '-').
One or more digits.
For example, all the following are valid numbers: ["2", "0089", "-0.1", "+3.14", "4.", "-.9",
"2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], while the following are not valid
numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]. Given a string s, return
true if s is a valid number
60 Write a program that should decode the given pattern and print the resulting expanded string.
Input: 2a3bc4dE5F2G7H
Output: aabbbcddddEFFFFFGGHHHHHHH
61 Given a string that contains a special character together with alphabets (‘a’ to ‘z’ and ‘A’ to ‘Z’),
reverse the string in a way that special characters are not affected.
Input: a!!!b.c.d,e'f,ghi
Output: i!!!h.g.f,e'd,cba
Input: str = “Ab,c,de!$”
Output: str = “ed,c,bA!$”