## Recent Interview Questions

- 0of 0 votes
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You can begin the journey with an empty tank at one of the gas stations.

Return ALL the starting gas station's index where you can travel around the circuit once

public List<Integer> canCompleteCircuit(int[] gas, int[] cost) {

}

- 0of 0 votes
Implement power function. The function should take two numbers as input (e.g. 2,3) and return 8 as output

See link below for hints and answer https://baquerrizvinotes.blogspot.in/2017/05/how-to-crack-amazoncom-technical.html

- 1of 1 vote
You have a array with integers:

`[ 1, -2, 0, 6, 2, -4, 6, 6 ]`

You need to write a function which will evenly return indexes of a max value in the array.

In the example below max value is 6, and its positions are 3, 6 and 7. So each run function should return random index from the set.

Try to implement with O(n) for computation and memory.

Try to reduce memory complexity to O(1).

- 0of 0 votes
You have a binary tree which consists of 0 or 1 in the way, that each node value is a LOGICAL AND between its children:

`0 / \ 0 1 / \ / \ 0 1 1 1`

You need to write a code, which will invert given LEAF and put tree in a correct state.

- 0of 0 votes
Given n1, n2 is the number after removing one digit from n1. Example, n1 = 123, then n2 can be 12, 13 or 23.

If we know the sum of n1 + n2, and find the possible values of n1 and n2.

public List<List<Integer>> getNumber(int sum){

}

- 0of 0 votes
Design copy-on-write string class

e.g. stringclass s("abc");

stringclass s1 = s; // no copy

s = "bcd"; // copy

- 0of 0 votes
Design and implement an algorithm to assign an unlimited M number of messages evenly among N number of servers (N will not change).

Input to the algorithm

A message contains a message identifier and a text string. The algorithm will be fed one message at a time.

There are N numbers of servers (N will not change), each can be identified by a unique id.

Output of the algorithm

When the algorithm is called to process a message, it should output the id of the server that the message is assigned to.

- 0of 0 votes
Given an ODD number, print diamond pattern of stars recursively.

`n = 5, Diamond shape is as follows: * *** ***** *** *`

void print(int n){

}

- 0of 0 votes
/From the input array, output a subset array with numbers part of a Fibonacci series.

// input: [4,2,8,5,20,1,40,13,23]

// output: [2,5,1,8,13]

public static List<Integer> getFibNumbers(int[] nums){}

- 0of 0 votes
The number of valid combinations of a strings for given input array a[], where a=>1, z => 26, and 0 <= a[i] <= 9

{1,1,1} => {aaa, ak, ka} => 3

{1,1,0} => {aj} => 1

- 0of 0 votes
/* The objective of this exercise is to build a road network connecting every pair of cities.

Each city should be connected to each other city once.

*/

public class Program

{

/* Your function RoadBuilder should return a list of new roads required to be built,

if the existing roads are given by builtRoads and the total number of

cities is nCities. Roads should not connect cities to themselves.

*/

public static int[][] RoadBuilder(int nCities, int[, ] builtRoads)

{

//implement the function here

return new int[0][];

}

public static void Main()

{

int[, ] test1 = new int[3, 2]{{0, 1}, {1, 2}, {3, 2}};

Console.WriteLine(RoadBuilder(4, test1)); // expected result should be {{0,2}, {0, 3}, {1, 3}}

}

}

- 0of 0 votes
Can multiple threads improve the time complexity to merge k sorted arrays? If so, how?

- 0of 0 votes
There is 3D space, limited with a cube, with edge=2000.

The center of coordinate system is point (0; 0; 0), so the maximum/minimal coordinate value is 1000/-1000.

There are 10000 points generated with discrete uniform distribution inside of K spheres, located in the cube.

Radius (R) of each sphere is 250.

Centers of spheres are located at the distance of not less than 2*R.

It is required to determine which point related to which sphere.

Input: array of 10000 structures, like:

struct Point {

int number;

int x;

int y;

int z;

}

where number is unique id of the point, x,y,z - it's coordinates.

Output:

array of 10000 structures, like:

struct Point {

int number;

int cluster_id;

}

where cluster_id is unique cluster id of a sphere that point belongs to.

Initially I considered a following solution:

1) Find Euclidian distance for each point from center of coordinates (0;0;0) to point's coordinates.

2) Sort this array of distances in descending order.

3) Get the point from the sorted array of distances and put in a new Set of Cluster Maximals.

4) Compare following point from the array to each value from the Set of Cluster Maximals (initially 1 value).

If it's Euclidian distance less than or equal to 2*R, then

mark this point as belonging to Kth cluster (range=1..N), otherwise add the point to the Set of Cluster Maximals.

5) Repeat step 4.

Two concerns I have:

1) There is an issue that my algorithm would work only in case if Claster Maximals are located on the surface of the spheres.

2) Plus, according to the task requirements, there could be the case when 2 spheres can have 1 and only common point.

I think in case if point belongs to 2 spheres, it is ok to mark it with cluster_id of any of these 2 shperes.

Could you please provide a proper solution to the task?

- 0of 0 votes
Products are identified by alphanumeric codes. Each code is stored as a string. We have three types of products:high priority, medium priority, and low priority. Given an array of product codes, sort the array so that the highest priority products come at the beginning of the array, the medium priority products come in the middle, and the low priority customers come at the end. Within a priority group, order does not matter. You are given a priority function which, given a product code, returns 1 for high, 2 for medium and 3 for low. This array may contain a large number of product codes, so do your best to minimize additional storage.

- 0of 0 votes
Products are identified by alphanumeric codes. Each code is stored as a string. We have three types of products:high priority, medium priority, and low priority. Given an array of product codes, sort the array so that the highest priority products come at the beginning of the array, the medium priority products come in the middle, and the low priority customers come at the end. Within a priority group, order does not matter. You are given a priority function which, given a product code, returns 1 for high, 2 for medium and 3 for low. This array may contain a large number of product codes, so do your best to minimize additional storage.

- 0of 0 votes
You get a password and a mapping for each character of the password. Print out all the possible mutations for the password.

E.g. password: "pass"

mapping:

p-> $

p-> P

a-> A

s-> /

s-> S

s-> &

Possible mutations would be for example "PASS" or "$A&S"

- 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
Given a Matrix A,

The rules for movement are as follows :

1. Can only move Right or Down from any element

2. Can only move within the row and column of element we start from intially.

3. You can only visit or cross an element if its value is lesser than the value of element you start from.

Find total number of elements one can visit, If one starts from an element A(i,j) where i-> row and j-> column.

Note : You have to print this output for each matrix element.

Input : Matrix

1 2 3

2 3 1

3 1 2

Output:

1 1 3

1 3 1

3 1 1

- 0of 0 votes
Gives an array of unsorted int (may have negative number),

rearrange the array such that the

num at the odd index is greater than the num at the even index.

example

giving 5, 2, 3, 4, 1, one of the expected result is 1,4,2,5,3,

please solve it in o(n) time, where n is the length of the array

public void rearrange(int[] nums){

}

- 0of 0 votes
`Given a string, find all possible combinations of the upper and lower case of each Alphabet char, keep the none Alphabet char as it is. example1 s = "ab", return: "Ab", "ab", "aB", "AB" example2 s = "a1b", return: "A1b", "a1b", "a1B", "A1B" public List<String> getPermutation(String s){ }`

- 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"); } }`

- 0of 0 votes
How many elements will the set object in the below snippet will contain after the program executes to last print statement?

`public class ShortSetTest { public static void main(String[] args){ Set shortSet = new HashSet(); for(short i = 0; i < 100; i++){ shortSet.add(i); shortSet.remove(i-1); } System.out.println(shortSet); } }`

- 0of 0 votes
Will the following class compile?

`public class WildCardGeneric { private static void add(List<? extends Number> list){ list.add(4); list.add(8); System.out.println(list.get(0)); } }`

- 0of 0 votes
What would the output of the following snippet?

`public class TrickyNum<X extends Number> { private X x; public TrickyNum(X x){ this.x = x; } private double getDouble(){ return x.doubleValue(); } public static void main(String[] args) { TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1)); System.out.println(a.getDouble()); } }`