## Java Interview Questions

- 0of 0 votes
Given a Binary string of 0s and 1s, and k, Find the number of different ways to get longest continuous streak of 1s. You can flip any k number of 0s to 1s.

Example:

1)Stirng is S=1010101, K=1

Result=3

1)Stirng is S=01010, K=3

Result=1

- 0of 0 votes
Given list of strings like “ crane, drain, refrain” and a pattern such as *an*

where * can match any number of chracters.

Return the matching word in an efficient manner.

Answer to above question : crane

- 0of 0 votes
design a zigzag iterator, implement the prev() and hasPrev function

- 1of 1 vote
Write a program to return nearest elements from a binary search tree for input element.

- 0of 0 votes
There is a dictionary already implemented. Write a method , which takes input String without space, to replace the characters from the strings which are not present in dictionary with –

Example: Dictionary – a*

………….Input- aaabaa

………….Output- aaa_aa

- 0of 0 votes
There is a dictionary already implemented. Write a method, which takes input String without space, to prints all subsets of the input string which is present in dictionary.

Example: Dictionary – a*

………….Input- aaabaa

………….Output- a,a,a,aa,aa,aaa,a,a,aa

- 0of 0 votes
Given two sorted linked lists, how can you combine them into one big sorted list? Do not create additional nodes.

- 0of 0 votes
There is going to be a sale during this month. You are interested in a particular item and you found that different Vendors have different prices during different time periods. You collected the following information:

`Vendor => (start date, end date, price) both sides inclusive A => (1, 5, $20) B => (3, 6, $15) C => (2, 8, $25) D => (7, 12, $18) E => (1, 31, $22)`

As you can see, there are conflicting entries. You need to print out a non-conflicting schedule of prices, taking the best price from each period:

e.g.

(1, 2, $20), (3, 6, $15), (7, 12, $18), (13, 31, $22)

- 0of 0 votes
Given a list of currency exchange rates like this:

EUR/USD => 1.2

USD/GBP => 0.75

GBP/AUD => 1.7

AUD/JPY => 90

GBP/JPY => 150

JPY/INR => 0.6

write a method`double convert(String sourceCurrency, double amount, String destCurrency);`

For example, convert(EUR, 100, INR)

The method should minimize the number of intermediate conversions.

- 1of 1 vote
Given a Map (representing an old phone key number and possible letters present there) and a sequence of keys return all possible combinations of strings that are possible to produce.

`Map<String, String[]> map = new HashMap<String, String[]>(); map.put("1", new String[] { "a", "b", "c" }); map.put("2", new String[] { "c", "d", "e" }); map.put("3", new String[] { "f", "g", "h" }); String in = "12"; List<String> mix(String in, Map<String, String[]> map)`

The result for "1,2" should be [ac, bc, cc, ad, bd, cd, ae, be, ce]

- 0of 0 votes
Write all the possible numbers returned from a calculator pad where a start number move in a L direction in any directions(1-2moves)

ie. From calculator pad. Start from 8 --> go in L shape (2up, 1right), and it returns 3

ie. Start from 2, (move 2 down, 1 left), it will be 7

ie. Start from 2(move 2 down, 1 right), it will be 9

ie. Start from 7(move 1 left, 2 up), it will be 2

ie. Start from 7(move 1 up, 2 left), it will be 6

- 0of 0 votes
Developing Java game

creating a RESTful service using which players can play a simple game described

below.

The game should have the following rules:

• The player has an infinite amount of coins.

• The player bets 10 coins to play a normal game round.

• In any round (free or normal), the player has a 30% chance of winning back 20 coins.

• In any round (free or normal), the player also has a 10% chance of triggering a free round where

the player does not have to pay for bet. The free round works in the same way as a normal round except

it costs 0 coins. The free round should follow immediately after the normal round.

• The player can both win coins and free round at the same time.

- 0of 0 votes
Testing Strings

Mr X and Mr Y, his friend are programmers and testers respectively working in the same company. So, once they faced the following scenario :

Mr X wrote an application that took as input some user data. The data the application took as input was a string in some strange language. That language consisted of

K

K distinct letters. However, due to some issues, the application got corrupted and one particular String among many was lost.

However, Mr X had seen that String once before it got lost. He remembers some info about it. Particu-larly, he remembers the lost String had length equal to

M

M.

Mr Y, being the chief QA person in his company needs to try and figure out the number of possible different possible candidate Strings for the lost String.

Mr X remembers N pieces of info. For each one, he gives you 2 numbers

L

L and

R

R and a number

Z

Z. He remembers for sure that the

Z

t

h

Zth letter of the language of the string did not occur between positions

L

L and

R

R inclusive of the lost String.

Input Format :

The first line contains

3

3 space separated integers

N

N ,

M

M and

K

K.

Each of the next

N

N contains

3

3 space separated integers, denoting

L

L ,

R

R and

Z

Z respectively.

Output Format :

You need to find and print the number of different possible candidate Strings for the lost String based on the info Mr X remembers. As the number of such Strings can be large, print it Modulo

10

6

+

3

106+3

Constraints:

1

≤

N

,

M

,

K

≤

10

5

1≤N,M,K≤105

1

≤

L

≤

R

≤

M

1≤L≤R≤M

1

≤

Z

≤

K

1≤Z≤K

Sample Input

1 2 26

1 2 1

Sample Output

625

- 0of 0 votes
Today Jin has given a task to Shino, Shino has to travel from cell

(

1

,

1

)

(1,1) to cell

(

N

,

M

)

(N,M) in a grid of size

N

∗

M

N∗M. But in order make this task interesting for Shino, Jin has decided to keep some special candies in some

K

K special cells of the grid, where each candy has an amount of happiness associated with it.

Shino can travel only in right & down direction in the grid, as he is too careful & does not want to fall out of grid. Now, we call the value of a path the happiness of all cells lying on the path. All non-special cells have happiness equal to

0

0.

Now, you need to find and print the sum of the values of all paths from

(

1

,

1

)

(1,1) to

(

N

,

M

)

(N,M), traveling only right and down to an adjacent cell.

As Shino is not good at counting help him find the answer.

Input Format

The first Line contains a single integer

T

T denoting number of test cases

The next line contains

3

3 space separated integers,

N

N,

M

M and

K

K where N * M is the size of grid & K denoting number of special cells

The next

K

K lines contains three integers

X

i

,

Y

i

,

H

i

Xi,Yi,Hi where

(

X

i

,

Y

i

)

(Xi,Yi) is cell coordinate &

H

i

Hi is the amount of happiness Shino will get from a candy at cell

(

X

i

,

Y

i

)

(Xi,Yi)

Constraints

1

≤

T

≤

3

1≤T≤3

1

≤

N

,

M

,

K

≤

10

5

1≤N,M,K≤105

1

≤

X

i

≤

N

,

1

≤

Y

i

≤

M

1≤Xi≤N,1≤Yi≤M

1

≤

H

i

≤

10

5

1≤Hi≤105

Output Format

For each test case you will output a single integer denoting the total amount of happiness Shino will get. As the answer can be quiet large you can output answer modulo

10

9

+

7

109+7

Sample Input

1

2 2 2

1 2 4

2 1 7

Sample Output

11

- -6of 6 votes
Folders and Cows Hackerrank Dropbox Question.

I haven't opened the link, so I don't know the prompt and I have only have 75mins to complete it starting when I begin the challenge.

- -1of 1 vote
Distinguish between final, finally and finalize keywords of Java language.

- 2of 2 votes
Print Common Suffix In Strings

Ex: cornfiled , Exfiled --- field

- 0of 0 votes
Write a method called createArray that accepts 4 parameters: number of rows, number of columns, a and b.

This method creates a 2-D array of number rows and columns as given in the parameter, then fills the array

with random values between a and b.

Write another method called checkColumn which accepts the position of the column, the min and max

elements of the array as parameter and returns true if the column contains both the minimum value and

maximum value, false otherwise.

Finding the min and max can be done in the main.

Sample run:

Enter number of rows and columns: 3 2

Enter 2 integers: 25 40

Array Elements:

26 32

39 28

27 30

Minimum: 26 Maximum: 39

Enter the position of the column to check: 0

Column 0 contains both min and max.

- 0of 0 votes
We need to develop a game that allows two players to throw a dice ten times and the player who gets the

highest sum wins the game.

Write a method sum(int[] values) that returns the sum of elements of the array values.

Write a method display(int[] values) that displays the elements of the array values.

Write a main method where you should:

a) Create two arrays of integers called player1 and player2 with size 10 both

b) For each player, generate ten random integers between 1 and 6 inclusively. The generated values

are added to the associated array of each player.

c) Display the generated values for each player.

d) Find and display the winner using the sum method.

Sample run

Generated numbers for player 1 are: 6 6 3 1 2 1 4 6 5 4

Generated numbers for player 2 are: 6 2 2 4 2 1 1 5 2 4

Player 1 wins with score 38

- 0of 0 votes
[Applying a curve to grades] Write a method called “applyCurve” that takes as a parameter a single-

dimensional array called “grades” of integers and a single value integer called “raise”. This method should

modify the array “grades“, which contains the grades of students in a way that each value in the array is

added to it the curve value “raise”. You should check that the grades after applying the raise does not exceed

100.

For example, if array “grades” contains the following values:

95 100 24 43 76 51 87

0 1 2 3 4 5 6

After calling the method “applyCurve” with a raise value +7 the array grades would be as follows:

100 100 31 50 83 58 94

0 1 2 3 4 5 6

Write a test program (main) that prompts the user to enter the size of an array, the grades and the required

curve and then displays the grades after applying a required curve.

Sample run

Enter the array size: 7

Enter the grades: 95 100 24 43 76 51 87

Enter the required curve: 7

The grades after applying + 7 are: 100 100 31 50 83 58 94

- 0of 0 votes
For given list of numbers find out triplets with sum 0

Input : arr[] = {0, -1, 2, -3, 1}

Output : 0 -1 1

2 -3 1

- 0of 0 votes
Given a String and dictionary of words, break the string in minimum space sentence.

Ex:

inputStr = "ilikefacebook"

dictionary = {"i","like","face","book","facebook"}

Possible Strings:

i like face book - 3 spaces

i like Facebook - 2 spaces - this is expected answer.

- 0of 0 votes
How do you reverse the words in a linked list?

For ex, Convert " H-e-l-l-o- -W-o-r-l-d " (There is a space between the word), into " o-l-l-e-H- -d-l-r-o-W "

Write a Java code to use as less Space Complexity as possible. (So not too many spaces should be used)

Linked List Structure:`class Node { char val; Node next; }`

- 0of 0 votes
Write a custom implementation of BlockingQueue using only intrinsic locking constructs in Java

- 0of 0 votes
Write a program to generate the anagrams of a word

- 0of 0 votes
Write a program to reverse a string

- 0of 0 votes
What is the output of following code snippet?

`public static void main(String[] args){ int[][] data = {{123},{4,5,6}}; int[][] copy = data.clone(); copy[0][0] = 100; System.out.println(data[0][0]); System.out.println(copy[0][0]); copy[1] = new int[]{300,400,500}; System.out.println(data[1][1]); System.out.println(copy[1][1]); }`

- 0of 0 votes
What would be the output of below code snippet?

`class Super { int index = 5; public void printVal(){System.out.println("Super");} } class Sub extends Super { int index = 2; public void printVal(){System.out.println("Sub");} } public class OopTest { public static void main(String[] args){ Super sup = new Sub(); System.out.println(sup.index + " "); sup.printVal(); } }`

- 0of 0 votes
Given a binary tree of integers, write code to store the tree into a list of integers and recreate the original tree from a list of integers.

Here's what your method signatures should look like (in Java):

List<Integer> store(Node root)

Node restore(List<Integer> list)

Example Tree:

5

/ \

3 2

/ / \

1 6 1

- 0of 0 votes
What would be the result of executing the below code snippet?

`public class CountDownLatchDemo { public static void main(String[] args)throws InterruptedException { final CountDownLatch latch = new CountDownLatch(3); latch.countDown(); latch.countDown(); new Thread(){ public void run(){ try{ Thread.sleep(3000); }catch(InterruptedException ex){ ex.printStackTrace(); } latch.countDown(); }; }.start(); System.out.println("Before"); latch.await(); System.out.println("After"); } }`