Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Coderpad Question Bank

The document is a comprehensive question bank containing programming problems categorized into simple, medium, and complex levels. It includes tasks related to string manipulation, number theory, data structures, and algorithms, with examples for clarity. Each problem is designed to test various programming skills and concepts.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Coderpad Question Bank

The document is a comprehensive question bank containing programming problems categorized into simple, medium, and complex levels. It includes tasks related to string manipulation, number theory, data structures, and algorithms, with examples for clarity. Each problem is designed to test various programming skills and concepts.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Question Bank

Simple

1. Check the String is Anargam or not


Ex: Dog, god
Cat, tac, act
angle - glean
tar-rat
car-rac

2. Check the given String is Panagram or Not.


ex: The quick brown fox jumps over the lazy dog.

3. Remove the duplicate words from the given


Sentence.
Ex:
a) Input: Good day day bye bye
output: Good day bye
b) input: greet the day user greet good day
output:greet the day user good

4. Find the weight of given String.


(A-Z/a-z) we have 26 alphabetic, consider the
position of a-z alphabets as 1-26. Based on that find
the weight of the given String.
Ex:
Given String: Apple
Output: 1+16+16+12+5 = 50
5. Write a program to print all String which starts as
"S" from the given String array.
Ex:
Input: {"apple","sample", "search","cat"}
output: { "sample", "search"}

6. Find Reverse String With Preserving Order.


Ex:
Input = abcd, output = dcba
Input = I am Java Developer, Output = r ep olev
eDavajamI

7. Write a program to find fibonacci series


8. Write a program to find the second smallest
number from a array of numbers recursively

9. Write a program to find first non-repeating


character from a input string

10. How to find is power of 10


Ex:
Input : 100
ouput: True (because 10 power 2 is 100)
Input: 80
Ouput: false (not power of 10)

11. Check the given number is power of 2.


Ex:
Input : 100
ouput: False (not power of 2)
Input: 4
Ouput: True (2 power 2 is 4)
12. Check the given number is power of 5.
Ex:
Input : 25
ouput: Truw (because 5 power 2 is 25)
Input: 80
Ouput: false (not power of 5)

13. Write the program to get second non-repeated


char from string

14. How to Count occurrences of each character in a


String in java

15. How to calculate total number of characters in


the String?

16. Find the maximum value in Array using Collection


17. Find out the numerology number for the given
name as mentioned below:
Each alphabet represented with one number as
given :
A,I,J,Q,Y - 1
B,K,R-2
C,G,L,S - 3
D,M,T - 4
E,H,N,X -5
U,V,W-6
O,Z-7
F-P-8
Ex. If input String is "S. KANAPATHY" then output
should be 4.
S. KANAPATHY ==> 3+2+1+5+1+8+1+4+5+1=31 (3+1)
=4

18. Problem Statement:

A robot moves on a grid. The grid has a X axis and Y


axis. The robot can take the following commands:
L - The robot moves one position left on X axis.
R - The robot moves one position right on X axis.
U - The robot moves one position up on Y axis.
D - The robot moves one position down on Y axis.

Considering the robot is at position 0,0 When given a


String like RRULDDDLR, what is the final location of
the robot on the grid?"

19. Find out the second smallest element in a given


array with appropriate logic that takes minimal time
and space complexity.
Array = {-1,-10,20,42,-13,0,100}

Output : -10

20. Given an array, print all the elements whose


frequency is one, that is they do not have duplicates.
Example: Array = [-1, -2, 3, 3, -2] Output = -1
21. Java 8: List<Integer> numbers=
Arrays.asList(1,43,15,66,87,89,9,10,22,34,23,89,54);

// Java 8
// 1. get only single digit number
//2. {even: evennumber, odd: oddnumbers}
//3. get all prime numbers

22. Create a custom class like Employee/Person with


2 to 3 fields and create the TreeSet and store two
objects and display the size of treeset

23. You have 2 curency notes {100,500}.


We need to make Rs 1200 out of these notes.
How many minimum notes required to get rs 1200.
Question Bank
Medium

1.. Print all perfect numbers between 1 to 100.


exp: Perfect number is a positive integer which is equal to the sum of its
proper positive divisors.
For example: 6 is the first perfect number
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6.
Hence 6 is a perfect number.

2. Check the given number is Strong number or not.


Strong number is a special number whose sum of factorial of digits is equal
to the original number.
For example: 145 is strong number. Since, 1! + 4! + 5! = 145

3. Print all Armstrong number between 1 to 1000.


An Armstrong number is a n-digit number that is equal to the sum of nth
power of its digits. For example,
6 = 61 = 6
371 = 33 + 73 + 13 = 371

"Implement the method that provided numerator and denominator will


return a string representing fraction's decimal form.
* Some fractions in decimal form have cyclic decimal points.
*
* Examples:
* 1/2 = 0.5
* 1/3 = 0.(3)"

4. Find Prime fraction of a number


if no. Is 6 prime fraction is 2,3
If no. Is 24 then prime fraction is 2,2,2,3
If no. Is negative or 1
then there is no fraction it should be empty
5. Question: For the given string such as "aabbbbbcc" print the longest
occurring character,index and number of times it occurs.
Ex: longest occurring character is b and length is 5 at index 2.

6. Given an array, print all the elements whose frequency is one, that is
they do not have duplicates. Example: Array = [-1, -2, 3, 3, -2] Output = -1

7. First Non-repeated character from the String

Ex:
Input : { "array", "apple", "rat"}
Output: y,a,r
8. For a given input 2D array of strings, find the maximum average grade,
round off to the nearest floor integer.

Input : { {"Rohit", "85"},


{"Rahul", "80"},
{"Amit","85"},
{"Rohit", "90"} }

Output : 87
Logic : Rohit's average grade is (85+90)/2 = 87.5 which when rounded off
to floor is 87

9. Find out unique substring of given length of string.


EX:
Input - (aab,2) , Output - aa, ab
Input- (aabc,2), Output - aa, ab,bc
Input -(abc,1), Output - a,b,c

10. Write a method that should return difference of characters (missing


char from given string) between given String and Input string.
eg. Given String = "abcdefghijklmnopqrstuvwxyz";
Input String = "Online test with GS client ";

11. Write a program for Panagram Detector.

* The sentence "The quick brown fox jumps over the lazy dog" contains
every single letter in the alphabet. Such sentences are called pangrams.
* Write a function called "findMissingLetters" which takes a String
`sentence`, and returns all the letters it is missing. Which prevent it from
being a pangram.
* You should ignore the case of the letters in sentence, and your return
should be all lower case letters, in alphabetical order.
* You should also ignore all non US-ASCII characters.
12. Find the median of two sorted arrays of different size.
Example:
Input :
Array 1: 2 3 6 7 9
Array 2: -1 4 8 10 = 5
Output : 6
Hint: The final sorted array would be - 1, 2, 3, 4, 6, 7, 8, 9, 10 The 5th
element of this array is 6.

13. Find out if String has all Unique Characters

14. Java program to sort array elements using bubble sort

15.Write a program to print all permutations of a given string.

16. Write a Java Program to Find missing Number in an Array


17. Write a Java Program to Remove Common Characters From Given
Strings

18. Java Program to Find Duplicate Character from a String and Count of
repetition

19.Print the count of repeated words from the file

20. Write a program which would take a string as input like 'aabccd' and
print the occurances of characters in the format a=2,b=1,c=2,d=1
21. Given an unsorted array A of size N, find a continuous sub-array which
sums up to S.
For example if the input array is [ 12,7,29,6, 2, 11,4,8] and the expected
sum is 8, then there are two possibilities [6,2] and [8]
if the expected sum is 19 there are two possibilites [12,7] and [6,2,11].
[11,8] is not a possibility since its not continous.

22.
Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing
order, return a sorted array of only the integers that appeared in all three
arrays in O(n). Make sure the time complexity is met.

Example 1:
Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]
Output: [1,5]
Explanation: Only 1 and 5 appeared in the three arrays

23. Given two sorted arrays, the task is to merge them in a sorted manner .
Example:1
Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8}
Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}
Example 2:
Input: arr1[] = { 3,7,9,12,21}, arr2[] = {6,8,15,18,27}
Output: arr3[] = {3,6,7,8,9,12,15,18,21,27}
Example 3:
Input: arr1[] = { 3,7,9,-12,21,12}, arr2[] = {6,8,15,-18,27}
Output: arr3[] = {-18,-12,3,6,7,8,9,12,1521,27}
Time complexity: O(n1+n2) where n1 and n2 are size of each array.
24. Problem Statement:
*
*Given a list of Strings, and an external order in which it needs to be sorted,
Sort the given list of *strings.
*For example:
*Input Strings : ""Ajay"", ""Raja"", ""Keshav"", ""List"", ""Set"",
""Elephant"", ""Drone""
*Sort order: TESARDLK
*Sorted Strings: ""Elephant"", ""Set"",""Ajay"", ""Raja"",
""Drone"",""List"",""Keshav""

25. Print the repeated top 5,10 words in decending order from the the
below paragraph.
input : ""Sunset is the time of day when our sky meets the outer space
solar winds. There are blue, pink, and purple swirls, spinning and twisting,
like clouds of balloons caught in a whirlwind. The sun moves slowly to hide
behind the line of horizon, while the moon races to take its place in
prominence atop the night sky. People slow to a crawl, entranced, fully
forgetting the deeds that must still be done. There is a coolness, a
calmness, when the sun does set.""
example:
the=7, a=4, of=3, to=3, when=2, There=2, in=2, is=2, sun=2, and=2"

26. Add two fraction a/b and c/d and print answer in simplest form.

Examples :

Input: 1/2 + 3/2


Output: 2/1

Input: 1/3 + 3/9


Output: 2/3

Input: 1/5 + 3/15


Output: 2/5
27. Players - Leaderboard
The leaderboard should display Players in the order of their level and
Scores.
Logic -Consider Player class with fields -> Name, Level and Score
Needs to sort the players list - first sort by Level and then by score"
28. long dotProduct( int[] array1, int[] array2 )
*
* Given two arrays of integers, returns the dot product of the arrays"

29. countLengthOfCycle(arr, startIndex)


*
* You are given an integer array of size N.
* Every element of the array is greater than or equal to 0.
* Starting from arr[startIndex], follow each element to the index it points
to.
* Continue to do this until you find a cycle.
* Return the length of the cycle. If no cycle is found return -1
*
* Examples:
* countLengthOfCycle([1, 0], 0) == 2
* countLengthOfCycle([1, 2, 0], 0) == 3"
30. You are an avid rock collector who lives in southern California. Some
rare
** and desirable rocks just became available in New York, so you are
planning
** a cross-country road trip. There are several other rare rocks that you
could
** pick up along the way.
** You have been given a grid filled with numbers, representing the
number of
** rare rocks available in various cities across the country. Your objective
** is to find the optimal path from So_Cal to New_York that would allow
you to
** accumulate the most rocks along the way.
** Note: You can only travel either north (up) or east (right).
** {{0,0,0,0,5}, New_York (finish) N
** {0,1,1,1,0}, < W E >
** So_Cal (start) {2,0,0,0,0}} S
** v
** The total for this example would be 10 (2+0+1+1+1+0+5)."
Complex

1. Write a program to print all possibilites to get the given number as


mentioned below:
Given number is 3.
Output:
1+1+1
1+2
2+1

2. Permutation combination :If a child is climbing a stair case with “n”


steps, find the maximum number of possibilities (how many different
ways he can climb the stair case)

3. Check if given path between two nodes of a graph represents a


shortest paths
Given an unweighted directed graph and Q queries consisting of
sequences of traversal between two nodes of the graph, the task is to
find out if the sequences represent one of the shortest paths between
the two nodes.
Examples:

Input: 1 2 3 4
Output: NO

4. Print Pascal Triangle , print the value for the given co-orinates
Ex:
1
11
121
1 3 3 1"
Given Co-Orinates(3,2) means answer should be : 2
5. Given a string array, each element containing a line of apache log file.
Every line starts with an IP address.
Write a program that returns the IP address which appeared most of the
times from the log file.

""123.123.23.123 - - [26/Apr/2000:00:23:48 -0400] \""GET


/pics/wpaper.gif HTTP/1.0\"" 200 6248 \""http://
www.jafsoft.com/asctortf/\"" \""Mozilla/4.05 (Macintosh; I; PPC)\""\n""
+ ""123.123.123.123 - - [26/Apr/2000:00:23:47 -
0400] \""GET /asctortf/ HTTP/1.0\"" 200 8130 \""http://
search.netscape.com/Computers/Data_Formats/Document/Text/RTF\""
\""Mozilla/4.05 (Macintosh; I; PPC)\""\n""
+ ""123.123.123.124 - - [26/Apr/2000:00:23:48 -
0400] \""GET /pics/5star2000.gif HTTP/1.0\"" 200 4005 \""http://
www.jafsoft.com/asctortf/\"" \""Mozilla/4.05 (Macintosh; I; PPC)\""\n""
+ ""123.123.123.123 - - [26/Apr/2000:00:23:50 -
0400] \""GET /pics/5star.gif HTTP/1.0\"" 404 1031 \""http://
www.jafsoft.com/asctortf/\"" \""Mozilla/4.05 (Macintosh; I; PPC)\""\n""
+ ""123.123.123.124 - - [26/Apr/2000:00:23:51 -
0400] \""GET /pics/a2hlogo.jpg HTTP/1.0\"" 200 4282 \""http://
www.jafsoft.com/asctortf/\"" \""Mozilla/4.05 (Macintosh; I; PPC)\""\n""
+ ""123.123.123.124 - - [26/Apr/2000:00:23:51 -
0400] \""GET /cgi-bin/newcount?jafsof3&width=4&font=digital&noshow
HTTP/1.0\"" 200 36 \""http:// www.jafsoft.com/asctortf/\""
\""Mozilla/4.05 (Macintosh; I; PPC)\""\n"";"

6. A guy uses elevator to reach his flat. But unfortunately elevator is not
working today and he became sad. Suddenly God came and made the
stairs magical, such that he can jump on it in a magical way. Initially he
can take 1 or 2 steps. If he jumps “x” steps at a time then in the next step
he can climb either x or x+1or x+2…… steps depending on his choice and
he must reach exactly on n'th step. Print all possisblities to reach his flat
by staircase.

7. Little girl has 5 ten rupee coins, 10 two rupee coins and 50 five rupee
coins. The needs 50 rupees out of this. Write a program to find all
possible ways the girl can make 50 rupees.
8. Find the Square root using Newton raphson method
Given number = N
Make an initial guess
Update the guess using below formula
New Estimate = Current Estimate - (F(Current Estimate) / F'(Current
Estimate))
where F(Current Estimate) = Current Estimate * Current Estimate -N
F'(Current Estimate) = 2 * Current Estimate
Repeat till you are close enough

9. In the given string, group the pattern such that the


input is ABABCABABCDE
output 8
Ex:1)ABABCABABCDE
Output:AB*C*CDE(length =8)
Ex:2)ABABCABABCDECDE
Output:AB*C*CDE*(length =9)
Ex:3)Input:ABCDABCE
Output:ABCDABCE(length =9)"

10.For the given Array, accept the target value as input and find out the
sum of minimum elements required to find the target value.
Example :
Array : { 1,2,3,4}
Target Value = 6
out put = 2+4 =6 return 2 element required .

Target Value = 12
output = 1+2+3+4 = 10 return -1 target value out of Scope .

11. Given two arrays. Arr1 = {1,2,3,8}, arr2 = {5,6,4,7}. Find a number
from each array to be interchanged to reach a common sum of all
elements for both arrays. Output: {1,5} or {3,7} or {2,6}
12. Given a knapsack with capacity C and two arrays w[] and val[]
representing the weights and values of N distinct items, the task is to
find the maximum value you can put into the knapsack. Items cannot be
broken and an item with weight X takes X capacity of the knapsack.
Examples:
Input: w[] = {3, 4, 5}, val[] = {30, 50, 60}, C = 8
Output: 90
We take objects ‘1’ and ‘3’.
The total value we get is (30 + 60) = 90.
Total weight is 8, thus it fits in the given capacity
Input: w[] = {10000}, val[] = {10}, C = 100000
Output: 10

13. Given an array arr[] of distinct integers from 1 to N. The task is to find
the minimum number of swaps required to sort the array.
Example:

Input: arr[] = { 7, 1, 3, 2, 4, 5, 6 }
Output: 5
Explanation:
i arr swap (indices)
0 [7, 1, 3, 2, 4, 5, 6] swap (0, 3)
1 [2, 1, 3, 7, 4, 5, 6] swap (0, 1)
2 [1, 2, 3, 7, 4, 5, 6] swap (3, 4)
3 [1, 2, 3, 4, 7, 5, 6] swap (4, 5)
4 [1, 2, 3, 4, 5, 7, 6] swap (5, 6)
5 [1, 2, 3, 4, 5, 6, 7]
Therefore, total number of swaps = 5

Input: arr[] = { 2, 3, 4, 1, 5 }
Output: 3"
14. Given the product of numbers, say x and Y. Find out the values are X
and Y, provided X and Y should be mirror numbers.

14. Prime Factorization, Given a number n, write an efficient function to


print all prime factors of n. For example, if the input number is 12, then
the output should be “2 2 3”. And if the input number is 315, then the
output should be “3 3 5 7”.

15. Given n non-negative integers representing an elevation map where


the width of each bar is 1, compute how much water it is able to trap
after raining.

Examples:

Input: arr[] = {2, 0, 2}


Output: 2"
16. List of Integer let say {9,7,4,30} , concatenate each element and find
out the maximum number.
17. Johnson Algorithm: Implementation of Johnson’s algorithm for all-
pairs shortest paths

18 Given a list of words, group them by anagrams


i/p: List of ""cat"",""dog"", ""god""
o/p: A set of sets of Anagrams: {{""cat""},{""dog"",""god""}}

19. dot product of arrays

20.The original list is : [(4, 5), (6, 1), (4, 5), (6, 1)]
List after removal of duplicates [(4, 5), (6, 1)]
21. A number of students, N of them, numbered 1 through N , sit in an
order in a circle. Walking
around the circle many times the teacher sings a song. when teacher
stop singiging the song,
the student who has teacher at back must leave the circle and are out of
the
game the length of the song is 2. Even though the circle thins out as this
game is played, the teacher keeps singing and stop singing
around the circle until one student remains. The remained student will
be the elected precident.

input arrays: [1,1,1], [2,2,1],[4,2,1],[100,2,73]

array[0] is number of students


array[1] is length of the song
array[2] is expected output

22."Given two arrays arr1 and arr2, the elements of arr2 are distinct, and
all elements in arr2 are also in arr1.
// Sort the elements of arr1 such that the relative ordering of items in
arr1 are the same as in arr2. Elements that do not appear in arr2 should
be placed at the end of arr1 in ascending order.

// Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]


// Output: [2,2,2,1,4,3,3,9,6,7,19]"

23."Given an array of n integers where each value represents the


number of chocolates in a packet. Each packet can have a variable
number of chocolates. There are m students, the task is to distribute
chocolate packets such that:

Each student gets one packet.


The difference between the number of chocolates in the packet with
maximum chocolates and packet with minimum chocolates given to the
students is minimum."
Design a ticketing system and list the available tickets
24. Input: {"1","A","B","C","2","3","F","V","5"} order all the numbers
then Uppercase letters, expected output:
{"1","2","3","4","5","A","B","C","F","V"}

25. Traveling Salesman Problem using Genetic Algorithm

26. Count the number of walks of length N where cost of each walk is
equal to a given number
Given a weighted undirected graph, Length of walks N and Cost X. The
task is to count the number of different walks W of length N such that
Cost(W) = X.
We define the cost of a walk W, as the maximum over the weights of the
edges along the walk.
The nodes are numbered from 1 to n. The graph does not contain any
multiple edges or self-loops.
27. You are an avid rock collector who lives in southern California. Some
rare and desirable rocks just became available in New York, so you are
planning a cross-country road trip. There are several other rare rocks
that you could pick up along the way. You have been given a grid filled
with numbers, representing the number of rare rocks available in various
cities across the country. Your objective is to find the optimal path from
So_Cal to New_York that would allow you to accumulate the most rocks
along the way.

Note: You can only travel either north (up) or east (right).
b) Consider adding some additional tests in doTestsPass().
c) Implement optimalPath() correctly.
d) Here is an example:
^
{{0, 0, 0, 0, 5}, New_York (finish) N
{0, 1, 1, 1, 0},
So_Cal (start) {2, 0, 0, 0, 0}} S
v
The total for this example would be 10 (2+0+1+1+1+0+5).

You might also like