## Software Developer Interview Questions

- 0of 0 votes
java program to interchage continous vowels in a string

ex:vowels

becomes vewols

continuous becomes cintonouuus

- 1of 1 vote
Consider an implementation of Strings consisting of an array where the first element of the array indicates the length of the String and the next elements represent the value of the ASCII characters in the String. Implement String concatenation and discuss shortcomings of this operation under this implementation of Strings.

- 0of 0 votes
Suppose that there exists a service to fetch Facebook posts through an interface defined by you, design the Facebook wall feed.

- 2of 2 votes
Given an array of ints and a value X, find wether there are 3 elements in the array such that their sum is X (return true/false).

- 0of 0 votes
Given two vectors that might be sparse, calculate their Dot product. You can assume any data structure to represent the vectors.

- 0of 0 votes
Given a matrix of characters where '_' represents an empty space, 'T' is a tree and 'H' is a house and a coordinate of that matrix, find the sum of the minimum distances from that coordinate to each house when considering that you can move through empty spaces and houses but not through trees.

- 0of 0 votes
Reverse a linked list.

- 0of 0 votes
Given two tree nodes and the root of a tree, find the distance between both nodes in the tree.

- 0of 0 votes
Given a String that contains parenthesis, remove the least amount of parenthesis from it such that it becomes balanced.

- 0of 0 votes
How would you store very large numbers that can't be store in a regular Integer or BigInteger, and make calculations

- 0of 0 votes
Design an application for people to find a place near them where they can join a team to play their favorite sport. Exp. Soccer teams open to join that are scheduled to play at a certain time and place. The application would search for the available teams to join within certain area playing within some given time period,

- 1of 1 vote
You have N toffee packets, each containing different number of toffees. The number of toffees contained in the ith packet is denoted by ci. You need to put these toffee packets in M boxes such that each box contains at least one toffee packet, and the maximum number of toffees in a box is minimum.

You can only choose consecutive toffee packets to put in a box.

Input

The first line of the input contains an integer T denoting the number of test cases. The first line of each test case contains two space separated integers N, M denoting the number of toffee packets and the number of boxes respectively. The second line of each test case contains N space separated integers c1, c2, ..., cN where ci denotes the number of the toffees in the ith toffee packet.

Output

For each test case, output a single line containing the maximum number of toffees in a box. Also, output -1 if such an assignment of toffee packets to boxes is not possible.

Constraints

1 ≤ T ≤ 20

1 ≤ N,K ≤ 100000

1 ≤ ci ≤ 1000

Example

Input:

1

4 2

10 3 5 7

Output:

13

Explanation

Below are the possible assignments of toffee packets to boxes.

10 [3 5 7]

10 3 [5 7]

10 3 5 [7]

For minimizing the maximum number of toffees in a box, we choose the second assignment, and hence the output should be 13

- 0of 0 votes
Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.

Let's index all the leaf nodes from the left to the right from 1 to 2^h. The exit is located at some node n where 1 ≤ n ≤ 2^h, the player doesn't know where the exit is so he has to guess his way out!

Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:

Character 'L' means "go to the left child of the current node";

Character 'R' means "go to the right child of the current node";

If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;

If Amr skipped two consecutive commands, he goes back to the parent of the current node

before executing next command;

If he reached a leaf node that is not the exit, he returns to the parent of the current

node;

If he reaches an exit, the game is finished.

Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?

Input

First Line contains T the number of test cases

The next T lines contains 2 integers h, n

Output

Output T lines each containing an integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.

Constraints

1 ≤ T ≤ 10

1 ≤ h ≤ 50

1 ≤ n ≤ 2^h

Example

Input:

1

2 2

Output:

2

Explanation

Example case 1. Amr would visit first root node then root->left node and then go to the root->left->right node which is the exit. hence 2 nodes visited before reaching the exit

- 0of 0 votes
You are walking down the escalator to catch a subway train. The escalator itself moves at a speed of Ve meters per minute. You can walk down the escalator at a relative speed of Vy meters per minute. The length of the escalator is L meters. Trains arrive T minutes apart. Let t be the time between your arrival to the station if you stand still on the escalator and the arrival of the last train before your arrival. Assume that t is a random variable uniformly distributed between 0 and T. Return the probability of catching an earlier train if you choose to walk down the escalator instead of standing still on it.

Input :

The first line of the input contains an integer Tc denoting the number of test cases. Each test case contains the following 4 lines

Ve - velocity of escalator

Vy - your relative velocity with the escalator

L - length of escalator

T - Time Period of Trains

Output

For each test case, output a single line containing the expected probability having an absolute or relative error less than 10^-6.

Constraints

0 ≤ Tc ≤ 5 * 10 ^ 7

1 ≤ Ve ≤ 1000

1 ≤ Vy ≤ 1000

1 ≤ L ≤ 10 ^ 5

1 ≤ T ≤ 10 ^ 6

Example

Input:

2

10

10

20

2

10

10

100

4

Output:

0.5

1.0

Explanation

Example case 1. If you stand still, it'll take you 20/10 = 2 minutes to reach the bottom of the escalator. If you choose to walk, it'll make you 20/(10+10) = 1 minute. In the second case you save 1 minute and in 50% of the cases it'll allow you to catch an earlier train.

Example Case 2. Here, if you choose to walk instead of stand still, you will save 5 minutes and you will certainly be guaranteed to catch an earlier train.

- 2of 2 votes
Find out if the given string forms a valid lottery number.

- A valid lottery number contains 7 unique digits between 1 and 59.

e.g.

4938532894754 (yes) -> 49 38 53 28 9 47 54

1634616512 (yes) -> 1 6 34 6 16 51 2

1122334 (no)

- 0of 0 votes
There are three row of houses. There are N houses in each row. Each house can be painted with three colors: red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses with following constaints

No two adjacent houses in a row have the same color.

Houses in a column have three different colors

You have to paint the houses with minimum cost. How would you do it?

Note: The cost of painting house 1 red is different from that of painting house 2 red in any row. Each combination of house and color has its own cost.

- 0of 0 votes
Write a function that takes an array of positive and negative integers and a number. This function should return true if the array contains a contiguous sub array that sums up to the number (2nd input).

He wanted an O(n) algorithm.

- 1of 1 vote
Password Suggestor: Replace s with $ and a with @ and produce all password suggestions.

For Example: Password : P@ssword, P@$$word,pas$word etc..

- 1of 1 vote
For a given Sum and N print all the combinations

For Example Sum = 16 and N=2

Then Answer :

16,0

15,1

14,2

13,3

12,4

11,5

10,6

9,7

8,8

7,9

6,10

5,11

4,12

3,13

2,14

1,15

0,16`private void recursivePrint(int sum, int n, int data[], int len, int originalSum) { if (n == 0) { int sum1 = 0; for (int j = 0; j < len; j++) { sum1 += data[j]; } if (sum1 == originalSum) { for (int j = 0; j < len; j++) { System.out.print(data[j] + " "); } System.out.println(); } return; } for (int i = 0; i <=sum; i++) { // System.out.println(" len: "+len+" sum: "+sum+" i: " + i+" n:"+n) data[len] = i; recursivePrint(sum - i, n - 1, data, len + 1, originalSum); } } }`

Need a better solution so that we can store previous results by using hashmaps

- 0of 0 votes
Perform an efficient DeepCopy of a linked list whose node is like below:

`public class Node { public int Value {get;set;} public Node Next{get;set;} public Node Random{get;set;} }`

The Random field points to any random node in the list.

- 0of 0 votes
write a function that randomly return only odd number in range [min, max)

public int getRandomOdd(int min, int max){}

- 0of 0 votes
Given an array of unique numbers. Find the number of pairs that make up the difference. This must be solved in under O(N^2)

`function getPairs(int[] array, int k){ HashMap<Integer,Integer> values = new HashMap<Integer,Integer>(); for(int i = 0; i < array.length; i++){ if(!values.containsKey(array[i])){ values.put(array[i],1); } } int pairs = 0; for(int i = 0; i < array.length; i++){ int diff = array[i] - k; if(values.containsKey(diff)){ pairs++; } } return pairs; }`

This will give O(N) time. O(N) Space

- 4of 4 votes
Given an integer array which represents the heights of adjacent vertical bars standing on the ground.

The width of each bar is 1. Now you have to pick two bars and remove all the remaining such that when

rain falls the water collected between two bars is maximum. Note that the distance between bars

remains the same after removing the remaining bars.

eg:

1. [10,5,6,12,14] ans: 30 (10*3)

2. [3 16 10 5 6 12 20 18] ans: 80 (16*(number of integers between 16 and 18)).

- 1of 1 vote
Phone Interview, New Grad - Software Developer

Imagine you are given 10,000 files each containing 1 Million integers. I would you sum all of them and give the final result?

---> Interviewer wanted to test scalability, distributed concepts.

He has written the basic code and wanted to improve upon that.

Here's the basic code.`public getSum(String[] file_names) { int sum = 0; for(String f: file_names) { sum = sum + sumOfFile(f); } return sum; }`

Questions:

What's wrong with above code? Ans: Integer overflow

How would you implement sumOfFile?

What if 'sumOfFile' takes lot of time to finish computing?

How do you fasten the program?

Overall scalability etc

- 1of 1 vote
1. Difference between arrays and link list

1.1 How to prepend each of the above with extra data

2. Hash-table. What datastructure to use to create one. How to resolve collision

- 0of 0 votes
/*

Amazon employees are encouraged to learn and be curious, and this means employees can transfer teams within the company easily.

This usually means they'll move to a new building, and for those who have parking, they'd need to swap parking spots.

The task is that given a list of employees who want to swap parking spots, write a function that can match them up 1 to 1.

The output can simply be tuples of aliases.

Each alias should only be matched once.

Input-

alias fromBuilding toBuilding

adrian building1 building2

john building2 building1

andrew building3 building2

william building4 building3

John building2 building4

Doe

output-

adrian,john

*/

- 0of 2 votes
The following code has a bug, find it and fix it

`Release() { m_refCount--; m.lock(); if (m_refCount == 0) { free(this); return 0; } m.unlock() return m_refCount; }`

- 1of 1 vote
Write a function that receives a string an returns a list of all the substrings (of length >= 2) composed by consecutive characters. E.g input : "BCCDE" , output: ["BC","CD","CDE","DE"]

- 0of 0 votes
Given a linked list rotate it on the Kth element. For example, given 1->2->3->4->5 the list should be transformed into 4->5->1->2->3

- 1of 1 vote
You have a string of numbers, i.e. 123. You can insert a + or - sign in front of ever number, or you can leave it empty. Find all of the different possibilities, make the calculation and return the sum.

For example;

+1+2+3 = 6

+12+3 = 15

+123 = 123

+1+23 = 24

...

-1-2-3 = 6

...

Return the sum of all the results.