Java Interview Questions
- 1of 1 vote
Answersmerge two binary search trees
- mh4wt@virginia.edu December 18, 2016 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java Trees and Graphs - 0of 0 votes
AnswersCreate a RESTful microservice that implements a card shuffling algorithm, as defined below. Should have evidence of test-driven development with unit tests. Use best practices of interfaces and generics for abstraction, preferably implementing a strategy pattern for deploy-time dependency injection of a shuffling algorithm.
- dbs.tkg December 11, 2016 in United States
Requirements:
· Create a microservice that stores and shuffles card decks.
· A card may be represented as a simple string such as “5-heart”, or “K-spade”.
· A deck is an ordered list of 52 standard playing cards.
· Expose a RESTful interface that allows a user to:
· PUT an idempotent request for the creation of a new named deck. New decks are created in some initial sorted order.
· POST a request to shuffle an existing named deck.
· GET a list of the current decks persisted in the service.
· GET a named deck in its current sorted/shuffled order.
· DELETE a named deck.
· Design your own data and API structure(s) for the deck.
· Persist the decks in-memory only, but stub the persistence layer such that it can be later upgraded to a durable datastore.
· Implement a simple shuffling algorithm that simply randomizes the deck in-place.
· Implement a more complex algorithm that simulates hand-shuffling, i.e. splitting the deck in half and interleaving the two halves, repeating the process multiple times.
· Allow switching the algorithms at deploy-time only via configuration.| Report Duplicate | Flag | PURGE
N/A Senior Software Development Engineer Java Object Oriented Design - 1of 1 vote
AnswersProgramming Challenge Description:
- abhinav.thegame October 17, 2016 in United States
Develop a service to help a client quickly find a manager who can resolve the conflict between two employees. When there is a conflict between two employees, the closest common manager should help resolve the conflict. The developers plan to test the service by providing an example reporting hierarchy to enable the identification of the closest common manager for two employees. Your goal is to develop an algorithm for IBM to efficiently perform this task. To keep things simple, they just use a single relationship "isManagerOf" between any two employees. For example, consider a reporting structure represented as a set of triples:
Tom isManagerOf Mary
Mary isManagerOf Bob
Mary isManagerOf Sam
Bob isManagerOf John
Sam isManagerOf Pete
Sam isManagerOf Katie
The manager who should resolve the conflict between Bob and Mary is Tom(Mary's manager). The manager who should resolve the conflict between Pete and Katie is Sam(both employees' manager). The manager who should resolve the conflict between Bob and Pete is Mary(Bob's manager and Pete's manager's manager).
Assumptions:
There will be at least one isManagerOf relationship.
There can be a maximum of 15 team member to a single manager
No cross management would exist i.e., a person can have only one manager
There can be a maximum of 100 levels of manager relationships in the corporation
Input:
R1,R2,R3,R4...Rn,Person1,Person2 R1...Rn - A comma separated list of "isManagerOf" relationships. Each relationship being represented by an arrow "Manager->Person". Person1,Person2 - The name of the two employee that have conflict
Output:
The name of the manager who can resolve the conflict Note: Please be prepared to provide a video follow-up response to describe your approach to this exercise.
Test 1:
Test Input
Frank->Mary,Mary->Sam,Mary->Bob,Sam->Katie,Sam->Pete,Bob->John,Bob,Katie
Expected Output
Mary
Test 2:
Test Input
Sam->Pete,Pete->Nancy,Sam->Katie,Mary->Bob,Frank->Mary,Mary->Sam,Bob->John,Sam,John
Expected Output
Mary| Report Duplicate | Flag | PURGE
IBM Software Engineer / Developer Coding Java Python String Manipulation - 0of 0 votes
AnswersThis questing was something related to parse trees. I really don't remember the semantics but needed to extract the complete sentences from the provided parse tress.
- abhinav.thegame October 13, 2016 in United States
Input: A full sentence: (S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))
Output: James is a boy eating sausages
Input: (NNS Sausages)
Output: Sausages
Input: (NP(DT a) (NN boy))
Output: a boy| Report Duplicate | Flag | PURGE
IBM Software Engineer / Developer Java - -1of 1 vote
AnswersFor a given string sentence, reverse it.
- abhinav.thegame October 13, 2016 in United States
Input : Hello World
Output : Dlorw Olleh
Input: How Are You Doing Today
Output: Yadot Ginod Uoy Era Woh| Report Duplicate | Flag | PURGE
IBM Software Engineer / Developer Java - 0of 0 votes
AnswersYou will be given a sequence of passages, and must filter out any passage whose text (sequence of whitespace-delimited words) is wholly contained as a sub-passage of one or more of the other passages.
- abhinav.thegame October 13, 2016 in United States
When comparing for containment, certain rules must be followed:
The case of alphabetic characters should be ignored
Leading and trailing whitespace should be ignored
Any other block of contiguous whitespace should be treated as a single space
non-alphanumeric character should be ignored, white space should be retained
Duplicates must also be filtered - if two passages are considered equal with respect to the comparison rules listed above, only the shortest should be retained. If they are also the same length, the earlier one in the input sequence should be kept. The retained passages should be output in their original form (identical to the input passage), and in the same order.
Input: For each test case a single line comprising the passages (strings) to be processed, delimited by | characters. The | characters are not considered part of any passage.
Output: A single line of filtered passages in the same |-delimited format.
Input1: IBM cognitive computing|IBM "cognitive" computing is a revolution| ibm cognitive computing|'IBM Cognitive Computing' is a revolution?
Output1: IBM "cognitive" computing is a revolution
Input2: IBM cognitive computing|IBM "cognitive" computing is a revolution|the cognitive computing is a revolution
Output2: IBM "cognitive" computing is a revolution|the cognitive computing is a revolution| Report Duplicate | Flag | PURGE
IBM Software Engineer / Developer Java - 0of 0 votes
AnswersThere is a DNA Strand having values as A , T , C , G.
- saurabh.desi.bhatt October 11, 2016 in United States
All combinations are present in the the file.
Write a method which takes starting mutation string , ending mutation string and string bank and calculates the minimum mutation distance required. But the condition is that either of the start or end must be present in the bank.
Input:
AATTGGCC is starting and TTTTGGCA is ending then mutation distance will be 3.
AATTGGCC - TATTGGCC - TTTTGGCC - TTTTGGCA as it takes three mustaion for start to reach the end string and for this , all intermediate string and final string must be present in the bank.
static int findMutationDistance(String start, String end, String[] bank) {
}| Report Duplicate | Flag | PURGE
Twitter Intern Java - 0of 0 votes
AnswersYou have a string of phrases present. For your simplicity consider them to be integer length.
- saurabh.desi.bhatt October 11, 2016 in United States
String s= " I am Tom"
will be stored in an interger array as [1,2,3] where each represents length of each word in the string.
Write a method to compute the longest subsequence such that it is less than given k value.
Input:
3 //length of array
1 //a[0]
2 //a[1]
3 //a[2]
4 // value of k
Output:
2
Input:
4 //length of array
3
1
2
1
4 //value of k
Output:
3
static int maximumLength(int[] a, int k) {
}| Report Duplicate | Flag | PURGE
Twitter Intern Java - 0of 0 votes
Answerswrite default and parameterized constructor accepting two variables
- D PRAVEEN KUMAR October 04, 2016 in India| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer Java - 0of 0 votes
AnswersYou have a big .csv file (say the size is 3GB). How efficiently you can process it and put the records in database?
- Anonymous July 26, 2016 in India| Report Duplicate | Flag | PURGE
deutsche bank Dev Lead Java - 3of 3 votes
AnswersA program stores total order numbers arrived at different time. For example, at 1.15 pm the program got 15 order, at 1.30 pm, the program got 20 order and so on.Now we need to design the data structure so that we can query the total orders we got in a time range efficiently. For this example, we can query as How many orders we have got between 1 and 2 pm? Ans will be 15+ 20 = 35
- gadha July 21, 2016 in India| Report Duplicate | Flag | PURGE
Amazon SDE-2 Data Structures Java Object Oriented Design - 3of 3 votes
AnswersGiven a file (which can be considered as a String with comma delimiter for the complexity of the question) of usernames and a value k, find top k usernames (with number of logins) who logged into the system the most.
- sambenison66 June 05, 2016 in United States
For example -
Input:
User (String) = user1, user4, user2, user1, user3, user1, user2, user3
k (int) = 2
Output:
user1 (3)
user2 (2)
user3 (2)
- Both user2 and user3 should be included since both has same number of logins
Write a java method to find the output with best time and space complexity.| Report Duplicate | Flag | PURGE
Amazon Software Engineer in Test Java - 1of 1 vote
AnswersGiven a list of numbers ex:(2,15,8) as a string with out any delimiters like space or ',' as str = "2158"; is there a way to parse the string an identify which one is double digit or single digit number ?? like 2,8 are single digit number and 15 is a double digit number
- coolcoder3 June 03, 2016 in India| Report Duplicate | Flag | PURGE
SDE1 Java - 0of 0 votes
AnswersBuild an in-memory DB that supports many commands and transactions.
- adch123 May 25, 2016 in United States
Similar to redis database (Redis.io)| Report Duplicate | Flag | PURGE
Database Java Object Oriented Design Software Design - -1of 1 vote
AnswersOO design for implementing table tennis game between two players? I just want the class diagram not the whole implementation. Only the class/interfaces names will help.
- shridhar.mali657 May 15, 2016 in United States| Report Duplicate | Flag | PURGE
ThoughtWorks Applications Developer Java - -1of 1 vote
AnswersThe input is being read in from a file called input.txt, in this format: 22|Data Structures|45
- nik April 01, 2016 in India
23|English|52
22|English|51
26|Data Structures|72
23|Data Structures|61
21|English|81
Each line consists of three fields "Student ID," "Subject," and "Marks." "Student ID" and "Marks" are integers and "Subject" is a string that does not contain '|' or newlines. There can be any number of students and up to 6 subjects. The fields are separated by the '|' character.
Currently, the existing program reads the input and creates an ArrayList<String> from the lines of input. Then it calls a method processData on this ArrayList, and prints the returned data to the output file. Unfortunately, processData currently does not do anything useful - it just returns an empty `Map.
You have to modify processData to compute the marks scored in each subject by the student with the lowest ID (who has actually appeared for that test). Thus, processData will returns a Map that maps each Subject to the marks scored. In other words, the map will contain each subject name as a key, and the marks scored by the student with the lowest ID as the value for that entry.
If your program is run with the input given above, it should produce the following output:
Data Structures: 45
English: 81
Please read this expected output carefully to understand exactly what you're expected to compute.
Plz help me solve urgent.| Report Duplicate | Flag | PURGE
Java Developer Java - 0of 0 votes
AnswersImplementation of Hashmap with overriding equals() and HashCode() method to display unique values in the class object.
- superb.saif March 18, 2016 in United States| Report Duplicate | Flag | PURGE
CDK Global Java - 0of 0 votes
AnswersJack love playing games, Gluttonous snake( an old game in Nokia era) is one of his favorite. However, after playing gluttonous snake so many times, he finally got bored with the game, so he changed the rules:
- mirrorme March 12, 2016 in United States
Rule 1: Write a code to find the Max sum path in a grid (2-D array), with dimension with n rows and m column (1<=n,m<=500)
Rule 2: In the 2D Array, each cell (elements) contains a value v in the array is from (-1<=v<=99999)
Rule 3. You can start from any position of the leftest column (border) of the array to the rightest(border) column of the array to calculate the Max Sum path.
Rule 4. You can move up, right, down, and CAN'T move left, and can visit each element only one time.
Rule 5.If the element is -1, it means the path is blocked, and you can't go through the path (calculate it in the sum), you have to choose other path to calculate the sum.
For example, if a 4*4 array grid
{{-1,3,2,1}
{2,-1,2,4}
{2,2,-1,3}
{4,2,1,2}};
The max sum path is : (from grid[4][0])
4-->up-->2-->left-->2-->down-->2-->left-->
1-->left-->2-->up-->3-->up-->4-->up-->1
and the sum is 4+2+2+2+1+2+3+4+1 =21
Thank you
Here is my code, I am new in Java and there is still lots of improvements
import java.util.*;
public class MainClass {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner rowDimension = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int firstInput = rowDimension.nextInt();
@SuppressWarnings("resource")
Scanner columnDimension = new Scanner(System.in);
System.out.print("Enter the number of columns: ");
int secondInput = columnDimension.nextInt();
//Input two number to generate 2D Array
Integer [][] array = new Integer[firstInput][secondInput];
//The purpose of the array is check the wall (cell value = -1)
boolean [][] visited = new boolean[firstInput][secondInput];
//Use Math.random() to generate the cell of the array
int[][] randomTable = new int[firstInput][secondInput];
for (int row = 0; row < firstInput; row++) {
for (int column = 0; column < secondInput; column++) {
// multiply by 1000000 to get a number between 0 and 99999
randomTable[row][column] = (int)(Math.random() * 1000000 -1);
System.out.print(randomTable[row][column] + " ");
}
System.out.println();
}
//Start form the left-down location of grid
int i = firstInput-1, j = 0;
visited[i][j] = true;
double sum = array[i][j];
while(true)
{
int max = -1;
int maxi = 0, maxj = 0;
//Case1 : choose path: UP
if(i-1 >= 0 && i-1<= firstInput-1 && j>=0 && j<= secondInput-1 && array[i-1][j] != null && array[i-1][j]>max && !visited[i-1][j])
{
max = array[i-1][j];
maxi = i-1;
maxj = j;
}
//Case2 : choose path: Down
if(i+1 >= 0 && i+1<= firstInput-1 && j>=0 && j<= secondInput-1 &&array[i+1][j] != null && array[i+1][j]>max && !visited[i+1][j])
{
max = array[i+1][j];
maxi = i+1;
maxj = j;
}
//Case3 : choose path: Right
if(i >= 0 && i<= firstInput-1 && j+1>=0 && j+1<= secondInput-1 && array[i][j+1] != null && array[i][j+1]>max && !visited[i][j+1])
{
max = array[i][j+1];
maxi = i;
maxj = j+1;
}
i = maxi;
j = maxj;
visited[i][j] = true;
sum += max;
//To the destination : Right-Up location of the grid
if(i == 0 && j == secondInput-1)
break;
}
System.out.println(sum);
}
}| Report Duplicate | Flag | PURGE
TATA Consultancy Services Software Developer Java - 0of 0 votes
AnswersToday is school picture day and everybody,
- ritikashah017 February 11, 2016 in United States
including the teacher, has lined up in a single line for the class picture.
Design an O(n log n) Java code that computes the minimum number of swaps necessary to be in order| Report Duplicate | Flag | PURGE
Amazon Intern Java - -5of 5 votes
AnswersGiven is a large paper with n different points with coordinates (x1, y1),(x2, y2), . . . ,(xn, yn).
- ritikashah017 February 11, 2016 in United States
Keep folding at 45 degres| Report Duplicate | Flag | PURGE
Amazon Intern Java - 2of 2 votes
Answersfind consecutive integers in a list that give you the biggest sum
- J@sper February 09, 2016 in United States
Like for -2 5 -1 7 -3 it would be 5 -1 7| Report Duplicate | Flag | PURGE
Google Java - 0of 0 votes
AnswersWithout using arrays, write a Console application that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the following numbers:
Smallest and largest values in the list,
Second smallest and second largest values in the list
This is what I have so far.public static void main (String[] args) {int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; int SecondMax = Integer.MAX_VALUE; int SecondMin = Integer.MAX_VALUE; Scanner s = new Scanner(System.in); while (true) { System.out.print("Enter a Value: "); int val = s.nextInt(); if (val == 0) { break; } if (val < min) { min = val; } if (val > max) { max = val; } } System.out.println("The smallest number is: " + min); System.out.println("The largest number is: " + max); System.out.println("The second smallest number is: " + SecondMin); System.out.println("The second largest number is: " + SecondMax); }
}}
- BrowerLot February 01, 2016 in United States| Report Duplicate | Flag | PURGE
UTMB System Administrator Java - 0of 0 votes
AnswersImplement following interface so that multi-put is atomic. Expect multiple producers and consumers inserting to and extracting from this queue implementation.
- scgupta January 30, 2016 in India
/**
* threadSafe bounded blocking queue implementation. Expected to be used in a
* Producer->Consumer pattern
*/
public interface MultiPutBlockingBoundedQueue<E> {
/**
* Sets the capacity of the buffer. Can be called only once. If called more
* than once or if the passed capacity is <= 0, throw an exception.
*/
public void init(int capacity) throws Exception;
/**
* Get the next item from the queue throws Exception if not initialized
*/
public E get() throws Exception;
/**
* Put the item to the tail of the queue. throws Exception if not
* initialized
*/
public void put(E obj) throws Exception;
/**
* Put the list of items in an atomic manner. The list can be more than the
* capacity throws Exception if not initialized
*/
public void multiput(List<E> objs) throws Exception;
}| Report Duplicate | Flag | PURGE
Linkedin Software Engineer Java - 0of 0 votes
AnswersFind a 1st non-repeated char in the string for e.g. if string is "Salesforce is the best company to work for” returns 'l'
- eminsqa January 27, 2016 in United States| Report Duplicate | Flag | PURGE
Salesforce Quality Assurance Engineer Algorithm Java - 0of 0 votes
Answersgiven a string, calculate the frequency of characters, output the array with the letter and frequency. (such as: for “abbcdc”, the output should be (a,1),(b,2),(c,2),(d,1))
- eminsqa January 26, 2016 in United States for AmazonMusic| Report Duplicate | Flag | PURGE
Amazon Quality Assurance Engineer Quality Assurance Algorithm Java - 2of 2 votes
AnswersI have a photo storage service. The actual photos are present in some storage and the index of these photos is present at some other place. The index is huge, say trillions of photos. Design the class for index node of each photo (with attributes like name*, date*, size*, accesscontrol, camera details, shot details, etc) such that 1. It is serializable. 2. For faster processing, I am interested in first 3 attributes. When deserializing the bytes of object, parse these 3 attributes i.e. instead of deserializing whole class, deserialize only part of the class (members marked by*), other members of class should be deserialized on demand with another call.
- AnonymousN January 10, 2016 in United States
How will you test the performance of your serialization/ deserialization?| Report Duplicate | Flag | PURGE
Amazon SDE-2 Java - 0of 0 votes
AnswersHow do you create your own garbage collector? How do you find whether objects in memory are orphans in order to purge them?
- harry December 02, 2015 in India| Report Duplicate | Flag | PURGE
Citrix System Inc Software Engineer / Developer Java - 0of 0 votes
AnswersWrite a method that takes in a positive integer, return the number of 2s between 0 and the input number.
- J@sper November 26, 2015 in United States
If the input value given is 13, it should return 2 as the number 2 and 12 are between 0 and 13.
If the input value given is 21, it should return 3 as the number 2,12 and 20 are between 0 and 21.| Report Duplicate | Flag | PURGE
Amazon Jr. Software Engineer Java - 0of 0 votes
AnswersIs java pass by value or pass by reference?
- Joey November 21, 2015 in India
Then he asked various question related to this.
What if i pass integer, array list or object will the change reflect in the original function.
In case of object will we have different behavior when i set it to null in the called function or when I call its method(setName("") for example ).| Report Duplicate | Flag | PURGE
Goldman Sachs Java Developer Java - 5of 5 votes
AnswersAsked me to write an API. Then ask:
- joyfeng November 19, 2015 in United States
Consider how the API could support 3rd party applications which need to perform some logic based on the structure and content of a filter in a type-safe manner.| Report Duplicate | Flag | PURGE
unknown Software Engineer / Developer Java