Java Interview Questions
- 0of 0 votes
Answers
- xankar February 26, 2017 in United Statesimport java.time.Duration; import java.time.LocalTime; import java.util.List; import java.util.Map; // Your goal is to write business logic for a very simple Restaurant booking system // You are encouraged to refactor exisiting code, create other classes, write helper methods etc // You also need to make sure that the implementation works correctly class Reservation { public String name; public int partySize; public LocalTime startTime; } class Table { public int tableNumber; public int maxPartySize; } class Restaurant { public List<Table> tables; public LocalTime openTime; public LocalTime closeTime; public Map<Integer, Duration> reservationDurationsPerPartySize; // Returns a Table if Reservation could be booked, null otherwise // Booking rules: // 1) Reservation could be made only when the Restaurant is open. // 2) Only one Reservation can be seatted a Table at any time. // 3) Reservation can be seatted only at a Table of the same or a bigger size. // 4) Reservation should stay on the same Table for the whole Duration. // 5) Reservation Duration is determined by PartySize. public Table bookReservation(Reservation reservation) { //TODO: } }
| Report Duplicate | Flag | PURGE
Opentable Backend Developer Algorithm Data Structures Java Problem Solving - 0of 0 votes
AnswersGiven set of N number of points/Co-ordinates[(x1,y1),(x2,y2), (x3,y3), (x4,y4), (x5,y5), etc] find if any of them form square.
- xankar February 26, 2017 in United States| Report Duplicate | Flag | PURGE
Pure Storage Backend Developer Algorithm Data Structures Java - 0of 4 votes
AnswersYou have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.
- Parth Patel February 21, 2017 in United States
{{
Test cases
==========
Inputs:
(int list) l = [3, 1, 4, 1]
Output:
(int) 4311
Inputs:
(int list) l = [3, 1, 4, 1, 5, 9]
Output:
(int) 94311
}}
My Solution:
{{
package com.google.challenges;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Answer {
public static int answer(int[] l) {
// Your code goes here.
ArrayList<Integer> list0 = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
int sum =0;
Arrays.sort(l);
for(int i = 0; i<l.length; i++){
if(l[i] % 3 == 0){
list0.add(l[i]);
}else if(l[i] % 3 == 1){
list1.add(l[i]);
}else{
list2.add(l[i]);
}
sum += l[i];
}
if(sum%3==0){
StringBuilder strNum = new StringBuilder();
for(int i = l.length-1; i >= 0; i--)
{
strNum.append(l[i]);
}
return Integer.parseInt(strNum.toString());
}else if(sum%3 == 1){
if(list1.size()>0){
Collections.sort(list1);
list1.remove(0);
}else if(list2.size() >= 2){
Collections.sort(list2);
list2.remove(1);
list2.remove(0);
}else{
return -1;
}
}else if(sum%3 == 2){
if(list2.size()>0){
Collections.sort(list2);
list2.remove(0);
}else if(list1.size() >= 2){
Collections.sort(list1);
list1.remove(1);
list1.remove(0);
}else{
return -1;
}
}
list0.addAll(list1);
list0.addAll(list2);
StringBuilder strNum = new StringBuilder();
Collections.sort(list0);
for(int i = list0.size()-1; i >= 0; i--)
{
strNum.append(list0.get(i));
}
return strNum.length() > 0 ? Integer.parseInt(strNum.toString()) : -1;
}
}
}}
But here I am able to pass 4 test cases out of 5. Therefore I am looking for scenario which is left to check.
Can someone help me?| Report Duplicate | Flag | PURGE
Google Software Engineer Google FooBar 24x7 Google chrome technical support number 1-888-201-2039 Arrays Computer Science Java Problem Solving - 0of 0 votes
AnswersAlex has recently decided to learn about how to design compilers. As a first step he needs to find the number of different variables that are present in the given code.
- misterraj.ruchit February 18, 2017 in India
So Alex will be provided N statements each of which will be terminated by a semicolon(;). Now Alex needs to find the number of different variable names that are being present in the given statement. Any string which is present before the assignment operator denotes to a variable name.
Input Format: :
The first line contains a single integer N
Each of the next N lines contains a single statement.
It is guaranteed that all the given statements shall be provided in a valid manner according to the format specified.
Output Format: :
Print the number of different variable name that are present in the given statements.
Sample Input
2
foo = 3;
bar = 4;
Sample Output
2
Explanation
Foo and Bar are only two variables used inside the statements so answer is 2.| Report Duplicate | Flag | PURGE
Aricent Software Engineer Java - 0of 0 votes
AnswersWrite a Code
- ruchitraj93 February 14, 2017 in India
Steve is going to throw a party at his place tonight.He needs to visit two shops near his home-the first shop is d1 meters away from his place,the second shop is d2 meters away from his place, and there are d3 meters between these two shops.Calculate the minimum distance he needs to walk to visit both both shops and return back home. Steve always start from his palce.He can only travel using these 3 routes.HE can use any route any amount of time necessary,the only thing he needs to achieve is the minimum distance.
Find the minimum distance Steve has to walk to visit both shops and return home.
input: 1,1,1
output: 4
input: 10,20,30
output: 60| Report Duplicate | Flag | PURGE
Practo Software Developer Java - 0of 0 votes
AnswersWrite a Java Program in which a class takes four integer arguments as input(a, b, c and d). Do addition of (a+b) on one thread, addition of (c+d) on another thread and multiplication of(a+b) * (c+d)) on main thread.
- Utsav February 06, 2017 in United States
Like: Thread1 = (a+b)
Thread2 = (c+d)
Main Thread = (Thread1 * Thread2)| Report Duplicate | Flag | PURGE
JP Morgan Senior Software Development Engineer Java - 0of 0 votes
AnswersLinkedList Reverse
- kumarami663 February 05, 2017 in India| Report Duplicate | Flag | PURGE
Amazon Quality Assurance Engineer Java - 5of 5 votes
AnswersGiven a string, find the longest substring with k distinct characters.
- getPDat February 03, 2017 in United States
e.g - “aaaabbbb”, k = 2, “aaaabbbb”
“asdfrttt” k = 3, “asd”, “frttt”
[Telephonic Question]| Report Duplicate | Flag | PURGE
Google Software Developer Java - 1of 1 vote
Answersinput = 8
output =
1 1 1 1 1 1 1 2
3 2 2 2 2 2 2 2
3 3 3 3 3 3 3 4
5 4 4 4 4 4 4 4
5 5 5 5 5 5 5 6
7 6 6 6 6 6 6 6
7 7 7 7 7 7 7 8
- vshah9031 January 25, 2017 in Indiaimport java.util.*; class Test { int n; Scanner sc; void readNumber() { sc=new Scanner(System.in); n = sc.nextInt(); } void printPattern() { // write your logic here } public static void main(String[] args) { Test t = new Test(); t.readNumber(); t.printPattern(); } }
| Report Duplicate | Flag | PURGE
Staff Engineer Java - 0of 0 votes
AnswersObject Oriented Design Problem
- tushar.1738 January 22, 2017 in India
--------------------------------
Design an OO parking lot. What classes and functions will it have. It should say, full, empty and also be able to find spot for Valet parking. The lot has 3 different types of parking: regular, handicapped and compact.
Use Case:
1. Customer are given a ticket that they can use to redeem to get their vehicle back
2. Parking spots come in three sizes, small, med, large
3. Thee types of vehicles, small[Two Wheeler], med[Car], large[Bus]
-a small vehicle can park in a small, medium, and large spot
-a medium vehicle can park in a medium and large spot
-a large vehicle can park in a large spot
4. There are multiple entry gate to park vehicle. So Vehicle should asign nearest posible parking spot| Report Duplicate | Flag | PURGE
Coding Java Object Oriented Design - 0of 0 votes
AnswersYou are given an array of integers. Find the minimum difference between two prime numbers(Positive or negative) in the array when present with minimum time complexity and provide the test data to test the this code.
- test January 22, 2017 in United States| Report Duplicate | Flag | PURGE
Amazon Quality Assurance Engineer Java - 1of 1 vote
AnswersRemove 3 consecutive duplicates from string.
- neerdis January 17, 2017 in India
INPUT:aabbbaccddddc
OUTPUT:cdc| Report Duplicate | Flag | PURGE
Amazon Developer Program Engineer Java - 0of 0 votes
AnswersSituation - You have millions of records(Strings) and user is typing "abc" in search box . Your task is to display strings from records in the sequence.
- surendrapandey3788 January 12, 2017 in India
e.g String in the record contain "abc" starting should appear first, then string which has "abc" in the second, should apprear later as below.
String - fabcsdf,asdfabc,dfadsfsdfabc,abckdf,ddfabc...
Displays suggestion like....
abckdf
fabcsdf
ddfabc
asdfabc
dfadsfsdfabc
Question - Which data structure you would use to stor this ?
And how will you implement to get this(shorted) result| Report Duplicate | Flag | PURGE
Nagarro Java Developer Java - 0of 0 votes
AnswersGiven 2 sorted linked lists, return a linked list that has all the elements and is sorted.
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswerGiven 3 strings "s" ssearch" and "sreplace", search string s for the substring ssearch and for every instance of ssearch you find, replace that part of the string with sreplace
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswersGiven an NxN Boolean matrix, find how many true regions there are in the matrixj
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswersCreate a basic minesweeper game that allows for board creation with custom height, width and number of mines. Create a <click> function that will take in a board location and return whether the user has won, lost, or the number of surrounding mines.
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswersGiven a string, print out all of the unique characters and the number of times it appeared in the string
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java String Manipulation - 0of 0 votes
AnswersWrite a code for reversing letters of string in java.
- kreetanshu December 26, 2016 in India| Report Duplicate | Flag | PURGE
Amazon Testing / Quality Assurance Java - 0of 0 votes
AnswersGiven a singly linked list of integers, write a function in java that returns true if the given list is palindrome, else returns false
- mh4wt@virginia.edu December 18, 2016 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java Linked Lists - -3of 3 votes
AnswersConvert an unordered tree to a binary tree
- mh4wt@virginia.edu December 18, 2016 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java Trees and Graphs - 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
Open Chat in New Window