Coderpad Question Bank
Coderpad Question Bank
Simple
Output : -10
// Java 8
// 1. get only single digit number
//2. {even: evennumber, odd: oddnumbers}
//3. get all prime numbers
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
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.
Output : 87
Logic : Rohit's average grade is (85+90)/2 = 87.5 which when rounded off
to floor is 87
* 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.
18. Java Program to Find Duplicate Character from a String and Count of
repetition
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 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.
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
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.
Examples:
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.
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.
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).