Java Interview Questions
- 0of 0 votes
Answerson last interview lunatic server solution asked 3 questions. one of them was very easy. the problem states that...
- PriyaDarad June 06, 2012 in United States
Given a stream of text, your task is to create a formatted paragraph out of it. The paragraph should be left justified and each line of text in the formatted paragraph should not exceed a given length. Each line should contain maximum possible characters.
In addition to the input text, a set of words with their possible hyphen positions will also be given. The hyphenation makes it possible sometimes to add incomplete words at the end of line, if the completed words overflow the line length limit. To indicate that this particular word is incomplete, a hyphen (-) is added at the end of such broken words. Remaining part of this word is placed at the beginning of the next line. The specified line length limit must be observed including the trailing hyphen character if any.
The input text will contain alphabets, digits, punctuation marks and spaces. The only white space character used is a blank space, which is used to separate words. Each line in a formatted paragraph begins with a non-blank character. Even if the input text has words separated by many spaces, your output should separate them by only single space or a line break.
Notes:
The list of hyphenated words will not include any punctuation marks that may occur in the text to be formatted. You may assume that the punctuation marks will occur only at the end of each word (alpha-numeric characters). That is there will not be words like They'll or Sita's anywhere in the input.
The word length will never exceed the line length limit.
The input text can always be formatted using the given constraints.
No word, hyphenated or otherwise will exceed a length of 20 characters.
Input specification:
First line has an integer n denoting the number of hyphenated words.
Next n lines give each of the hyphenated word with possible hyphen break positions in the word by a hyphen itself. For example, "hyphenate" is given as "hy-phen-ate" and "formatting" is given as "for-mat-ting".
Next line will be an integer denoting the maximum number of characters per line in the formatted paragraph, i.e., the line length limit.
Last line contains the text to be formatted into a paragraph. The entire text will be terminated by a new line. The length of text will not exceed 256 characters.
Output specification:
The output should be a sequence of characters, which begin each line of the formatted paragraph followed by a single new line character. Thus, there will be those many non-white space characters as there are lines in the formatted paragraph.
Sample Input and Output:
Input:
4
con-cept
pro-gram-ming
ob-vi-ous
im-pos-si-ble
25
Most people find the concept of programming obvious, but the doing impossible.
Output:
Mcos
Note : Candidate can not get characters of indices, which are multiple of 24.
you have to make program in java with proper coding and follow the problem statement.| Report Duplicate | Flag | PURGE
Lunatic Server Solutions Developer Program Engineer Java - 0of 0 votes
AnswersWithout using loops, write a function to print 1 to 500 in serial order.
- panshul21 June 02, 2012 in India for RSA| Report Duplicate | Flag | PURGE
EMC Software Engineer in Test Java - 0of 0 votes
AnswersWrite a String Reverser (using Recursion) and write JUnit test for the reverse method.
- panshul21 June 02, 2012 in India for RSA| Report Duplicate | Flag | PURGE
EMC Software Engineer in Test Java - 0of 0 votes
AnswersWrite a java program to count number of words in a file.
- panshul21 June 02, 2012 in India for RSA| Report Duplicate | Flag | PURGE
EMC Software Engineer in Test Java - 0of 0 votes
AnswersWithout using loops, write a function to print 1 to 500 in serial order.
- panshul21 June 02, 2012 in India for RSA| Report Duplicate | Flag | PURGE
EMC Software Engineer in Test Java - 0of 0 votes
AnswersWrite a String Reverser (using Recursion) and write JUnit test for the reverse method.
- panshul21 June 02, 2012 in India for RSA| Report Duplicate | Flag | PURGE
EMC Software Engineer in Test Java - 0of 0 votes
AnswersLunatic server solutions asked this problem for fresher recruitment in india.
- PriyaDarad May 31, 2012 in United States
On monday we asked this problem for freshers.
there was a very easy problem.
problem states that...
You are required to write a program to do simple pattern matching, in a string. The string in which the pattern is to be found, henceforth referred to as Master String, can be composed of the following character sets:
a-z {any character in the range a to z}
A-Z {any character in the range A to Z}
0-9 {any digit in the range 0 to 9}
The pattern to be matched is also a string, henceforth referred to as Scan String. The Scan String can be composed of the above character sets, as well as two special characters, which are: '.' and '*'. The dot ('.') is to be interpreted as matching any one character from the above character sets and the star ('*') is interpreted as matching zero or more of the previously matched character. The Scan String may or may not contain the special characters.
A pattern is considered matched when the longest substring that satisfies the pattern is returned.
Input Specification
The first line of input contains the Master String.
The second line of input contains the Scan String.
Both Master String and Scan String will not be greater than 80 characters in length.
Output Specification
Your program must output the length of the longest substring matched. If there is no match, then the length of the matched substring is zero.
Input:
abcd23Abdaaaa4g9
.*Abd. { dot-star-A-b-d-dot }
Output:
10
Input:
aaadaaabbbb129cd
a.*d { a-dot-star-d }
Output:
16
Input:
0AbC1dEf2GhI3jKl4MnO5pQr6StU7vWx8Yz9
3JkL4 { 3-J-k-L-4 }
Output:
0
Input:
090m90mm90mmm90mm90m909
0m*9
Output:
5| Report Duplicate | Flag | PURGE
Lunatic Server Solutions Developer Program Engineer Java - 0of 0 votes
AnswerGreen Bricks asked this question for the post of project leader for their upcoming project.
- John May 31, 2012 in United States for 1 alpha
project engineer has to be in java.
green bricks also offered attractive salary.
the problem was as follows:
Given a set of figures and a board (a 2D array), you are required to figure out if all the figures can be placed on the board such that no two figures overlap each other. A figure can be of any shape and is represented using a matrix of 0s and 1s. The 1s in the matrix indicate the solid part that makes the figure.
Note:
Overlapping of 0s of figure A with 1s and 0s of figure B is allowed.
The figures provided are to be fitted as is without any rotation.
There is only one figure per matrix.
The matrix describing the figure will not have any empty row or column.
Input specification:
The first line contains two integers M and N (0<=50 and 0<=50), the dimensions of the board. The board is empty at the start.
The second line contains an integer F (0<=F<=10), indicating the number of figures followed by F figures.
Each figure has two integers R and C, the dimensions of the matrix containing the figure followed by R lines containing C integers 0's or 1's separated by a space.
Output specification:
If all the given figures fit onto the board then print YES followed by the number of empty cells on the board, separated by a space. If all the pieces cannot be fitted on the board together then print NO.
Sample Input and Output:
Input:
4 4
3
1 1
1
4 3
0 0 1
1 1 1
0 0 1
0 1 1
4 3
1 1 1
1 0 0
1 1 1
1 0 0
Output:
YES 0
this was the sample input output case.
you have to make the program for this which should also work on the following test cases.
Test Case 1:
4 4
3
1 1
1
4 3
0 0 1
1 1 1
0 0 1
0 1 1
4 3
1 1 1
1 0 0
1 1 1
1 0 0
Test Case 2:
4 4
2
4 4
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
4 4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Test Case 3:
8 8
8
2 2
1 1
1 1
2 2
1 1
1 1
2 2
1 1
1 1
2 2
1 1
1 1
2 2
1 1
1 1
2 4
1 1 1 1
1 1 1 1
4 4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
4 8
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0
you have to figure out the answers.
regards,
john| Report Duplicate | Flag | PURGE
Green Bricks Software Engineer / Developer Java - 0of 0 votes
AnswersA newly established university in New York has its campus located in a remote corner of the city. The university offers several courses and students are required to take a certain number of these courses. Each course publishes its schedule of lectures on a day-wise basis, i.e., it publishes the days of the week on which it holds lectures. Each course chooses exactly three days of the week for lectures. Travelling to the university is a nuisance and all the students who attend courses there, seek to minimise the number of days that they should have to travel. Given each course's schedule, you have to help the students decide on a combination of courses, such that they minimise the number of days that they have to travel to the university.
- Anonymous May 30, 2012 in United States for test alpha
The university has strict attendance norms and it insists that students should attend all the lectures that their respective course offers.
Assume that the number of courses required will never be greater than the number of courses offered. In addition, there will be a unique combination of days, which allows the students to take the required number of courses and minimise travel.
Input specification:
First line will contain total number of courses (T) and the number of courses you are required to register (R) for.
The next T lines will each contain name of the course and those days on which the lectures for the course are held. The first word on each line is the name of the course and the rest of the words are three-letter words signifying the days of the week. E.g., "Physics MON WED FRI".
Output specification:
You should output the names of the days on which the student is required to go to the University to attend lectures. These should be printed on a single line in sorted order (SUN to SAT) with a single whitespace as a separator. Terminate the output with a newline.
Sample Input and Output:
Input:
6 3
Physics MON WED TUE
Chemistry TUE WED THU
Biology WED SAT THU
Economics MON FRI WED
English TUE WED FRI
History MON THU FRI
Output:
MON TUE WED FRI
Input:
6 2
Physics SUN TUE WED
Chemistry TUE WED THU
Biology THU WED FRI
Economics MON WED FRI
English THU TUE WED
History FRI MON THU
Output:
TUE WED THU| Report Duplicate | Flag | PURGE
Green Bricks Software Engineer / Developer Java - 0of 0 votes
Answerswrite a program that displays the its source code as its output.
- balaji.paduchuru May 28, 2012 in India| Report Duplicate | Flag | PURGE
ION idea Developer Program Engineer Java - 0of 0 votes
Answerscreate an array and remove all the duplicates in the array.no duplicates are allowed in the array.display the contents of the array finally after the duplicates are removed.
- balaji.paduchuru May 28, 2012 in India| Report Duplicate | Flag | PURGE
ION idea Developer Program Engineer Java - 0of 0 votes
AnswersAn older has a very old computer at his house. It is so old that there is no notion of virtual memory in the operating system. Instead it uses a simple memory allocation technique called the 'Best Fit Algorithm'. The BFA works like this:
- PriyaDarad May 26, 2012 in United States
Whenever a request comes in for some memory space, the OS looks for the smallest, continuous empty space, which can satisfy it, and allocates a portion of this region to the program making the request. If such space cannot be found, the program is terminated and all the memory used by the program is freed. Any program can get terminated due to two reasons:
The program exits
Enough memory is not available to start the program or keep it running
All memory requests that come in at the same time-instant are processed in ascending order of process-id. If while processing, a program is terminated for lack of memory, all memory held by it is freed before processing any other request and no further memory requests by the terminated program are considered. All termination is also done in order of process-id.
You have to write a program to simulate this algorithm and print out the number of processes that were terminated due to lack of memory.
Note:
1.No process will request for memory before its start-time or after its end-time.
2.All memory sizes are specified in KB.
3.The memory is linear in nature, i.e., addresses do not wrap around. It starts at zero and goes up to N-1 KB.
4.At a given time unit, all processes that have reached end-time will be terminated (and memory freed) before other memory requests are serviced.
5.The total free space available in memory might be enough to satisfy a request, however the OS will terminate the process if a continuous region of empty space is not found
Input specification:
First line has an integer N (0 < N <= 1000), size of the memory in KB.
Second line of input will have integer P (0 <= P <= 20), the total number of processes to be run on the system.
Next P lines will have data for each process in the following format:
<process-id> <start-time> <end-time> <initial-memory>
These input lines will be sorted in ascending order of the process-id.Process-id will be unique integers greater than zero.
Next line will have integer R (0<= R <= 20), which specifies the subsequent memory requests by any process.
Next R lines have the following format:
<process-id> <request-time> <memory-required>
Output specification:
An integer specifying the number of processes that were terminated due to lack of memory
Sample Input and Output:
Input:
100
4
1 3 7 20
2 0 4 30
3 0 3 70
4 0 10 25
2
3 2 30
2 2 10
Output:
2
Input:
10
2
1 0 8 4
2 0 3 2
3
1 1 2
2 1 2
1 4 4
Output:
1| Report Duplicate | Flag | PURGE
Lunatic Server Solutions Computer Scientist Java - 0of 0 votes
AnswerIf you implement an interface compile asks you to provide implementation of those methods. But in case of object.clone() how come this is inverse? Means you have overridden clone() method but your class doesn't implements copyable. How compiles does this mapping to tell user that CloneNotSupported ??
- coder May 22, 2012 in -| Report Duplicate | Flag | PURGE
Software Engineer / Developer Java - 0of 0 votes
AnswersWhat is difference between following interface and abstract class:
public interface MyInterface { public int get1(); public int get2(); public int get3(); } public abstract class MyAbstract { public abstract int get1(); public abstract int get2(); public abstract int get3(); }
Interviewer was not convinced with following answers, he wanted to hear something else:
- coder May 22, 2012 in -
1. I have to extend MyAbstract and then I cannot have more extends, whereas in case of implementing MyInterface I am open to have inheritance.
2. I have to provide implementation of all three methods if used "implements MyInterface", whereas in case of "extends MyAbstract" I am open to carry forward abstractness.
3. Design perspective: All libraries work on interfaces not on abstract classes, it is good design practice to use interfaces so that at any time in future I can create any class (implements MyInterface) that can be used in some method of library. (basically same as point one)
What else there could be? I am not concerned with the variables in interface/abstract class etc. How to decide which one to use?| Report Duplicate | Flag | PURGE
Software Engineer / Developer Java - 0of 0 votes
AnswerDr. Alberquert invented the following three devices to set up a simple communication network:
- dev May 22, 2012 in United States
Synthesizer (S), which produces signals continuously and transmits (propagates / passes on) them to neighbouring cells but cannot receive signals.
Receiver (R), which can receive signals from neighbouring signal sources but cannot produce or propagate signal.
Transmitter (T), which is capable of both receiving signal from and transmitting signals to neighbouring cells. Transmitters also are NOT capable of producing signals.
These devices are laid in a matrix formation. Signals are propagated at the rate of one cell per time unit. The absorption rate of a receiver is unlimited and so also the transmission and absorption rate of a transmitter unlimited. For simplicity we shall ignore the exact nature of signals being produced and consider them uniform across sources. Devices on the extreme right side can also communicate with the extreme left hand side device present in the same row (see fig 2). Similarly a device on the extreme top can also communicate with a device at the extreme bottom if they are present in the same column.
Please Note:
Neighbourhood is a four cell neighbourhood, i.e, the neighbourhood of a cell is defined by cells to its NORTH, SOUTH, EAST and WEST (see fig 1).
All Synthesizers will start to produce signals as soon as the simulation begins.
There could be multiple Synthesizers in a matrix arrangement.
Your task is to write a program that would take a matrix containing devices and output the time at which each receiver/transmitter receives its first signal.
Input specification:
The first line has two integers M and N indicating the number of rows and columns of the matrix. 0 < M, N <= 20
M lines follow the first line. Each of these M lines contains N characters and a terminating new line. Each character is one of S, T or R.
Output specification:
The output should be a matrix of M rows with each row containing N integers separated by spaces indicating the minimum time required for the signal to reach the corresponding device. The output for cells containing Synthesizers is 0. For devices that never receive any signal, print -1.
Sample Input and Output:
Input:
3 4
SRTR
TTTT
TTTS
Output:
0 1 3 1
1 2 2 1
1 2 1 0
Input:
2 3
RTT
TTR
Output:
-1 -1 -1
-1 -1 -1| Report Duplicate | Flag | PURGE
Java - 0of 0 votes
AnswersGiven a matrix of order M x N containing 1.s and 0's, you have to find the number of maximal squares that can be formed. A square is formed by grouping adjacent cells containing 1. A maximal square is one that is not completely contained within another square. Maximal squares that overlap partially are to be counted separately. Unit squares (length of side = 1) should be also counted.
- dev May 22, 2012 in United States
Note that squares are filled, i.e. they cannot contain 0.s.
Number of maximal square: 6
Input specification:
The first line consists of integers M and N, the number of rows and columns of the matrix.
0 < M and N <=40
Next M lines contain N characters from the set {0, 1}.
Output specification:
An integer representing the number of maximal squares that can be formed followed by a newline.
Sample Input and Output:
Input:
4 5
11001
11110
11011
11001
Output:
9
Input:
2 2
10
11
Output:
3| Report Duplicate | Flag | PURGE
Java - 0of 0 votes
AnswerKingKong, the largest living ape, escaped from Xanadu lab into a forest. The forest is filled with dangerous animals, which will attack and kill human beings that venture too close to them. You are required to help the scientists find a way to get to KingKong safely. The table below gives the minimum distance one must keep from each species for safety.
- dev May 22, 2012 in United States
Animal Name
Code
Safety distance (unit cell)
Lion L 1
Panther P 2
Both, the animals as well as the scientists can move only in horizontal or vertical directions. So, in the figure, the cells shaded gray are the cells into which the scientists must NOT venture, in order to be safe. The arrow shows that the panther is one vertical and one horizontal (a total of two) cell away from it.
A snapshot of the forest is obtained from a satellite picture in terms of an MxN matrix, which is the input to your program. This snapshot gives the location of various animals in the forest. Some cells might contain trees, which merely block the path of the scientists. The cells within the snapshot are marked by:
Animal code indicating the animal present in the cell
'T' in case of a tree present in the cell
'S', which indicates the start position of the scientists
'K', KingKong's location
'#', All other cells, which are empty
The output of your program should be the number of steps in the path that the scientists should take.
Notes:
There will be a unique path, if one exists.
The entire path including the starting position of the scientist as well as KingKong's location must be safe.
Input specification:
The first line contains two integers M and N the number of rows and columns. The next M lines contain N characters from the set {'L', 'P', 'T', 'S', 'K', '#'} as explained above
Output specification:
An integer specifying the length of the path, from the starting position of the scientist, to KingKong's position both inclusive. If KingKong is not reachable safely, then output the integer '-1'.
Sample Input and Output:
Input:
7 6
TLT#PP
LL####
LL#K##
TT##TT
TT#TTT
T###TT
TTTSTT
Output:
7
Input:
11 9
LL#######
L##TTTTT#
LKTTTTTT#
####P#TT#
###PP#TT#
####P#TT#
######TT#
L##T##T##
T##T####S
T####T###
TTTTTTLL#
Output:
-1| Report Duplicate | Flag | PURGE
Java - 0of 0 votes
AnswersWrite a java program to do the breadth first search of a graph.
- get.santhoshkrishna May 19, 2012 in United States| Report Duplicate | Flag | PURGE
InMobi Java Trees and Graphs - 3of 3 votes
AnswersQ1.- Written exam (Amazon, Bangalore)
- Nitin Gupta May 12, 2012 in India
Given a singly link list and a number 'K', swap the Kth node from the start with the Kth node from the last. Check all the edge cases.
Sample Input: 1->2->3->4->5->6->7->8 and K = 3
Sample Output : 1->2->6->4->5->3->7->8
Sample Input: 1->2->3->4->5->6->7->8 and K = 10
Sample Output: print error "LIST IS OF LESSER SIZE".| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm C C# C++ Coding Data Structures Java Linked Lists - 0of 0 votes
AnswersQ2. F2F Round-1, Amazon(Bangalore)
- Nitin Gupta May 12, 2012 in India
Given an array of integers having the property that first that array is strictly increasing then it is strictly decreasing, You have to search for a given number.
Constraint: Minimize the complexity| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm Arrays C C# C++ Coding Data Structures Java - 0of 0 votes
AnswersQ1. F2F Round 1 Amazon(Bangalore)
- Nitin Gupta May 12, 2012 in India
Given a character array as input. Array contains only three types of characters 'R', 'G' and 'B'. Sort the array such that all 'R's comes before 'G's and all 'G's comes before 'B's.
Constraint :- No extra space allowed(except O(1) space like variables) and minimize the time complexity.
You can only traverse the array once.| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm Arrays C C# C++ Coding Data Structures Java Sorting - 0of 0 votes
AnswersQ4. Written Exam Amazon(Bangalore)
- Nitin Gupta May 12, 2012 in India
Given an array of integers A[1....n-1] where 'N' is the length of array A[ ]. Construct an array B such that B[i] = min(A[i], A[i+1], ......., A[i-K+1]), where K will be given.
Array B will have N-K+1 elements.
Constraint: Extra space allowed O(K) and time complexity allowed O(N.K) or lower.| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm Arrays C C# C++ Coding Data Structures Java Sorting - 1of 1 vote
AnswersQ3. Written Exam Amazon(Bangalore)
- Nitin Gupta May 12, 2012 in India
Given a singly linked list which may or may not contain loop and loop may or may not start from the head node. Count the number of elements in the linked list.| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm C C# C++ Coding Data Structures Java Linked Lists - 0of 0 votes
AnswersQ2. Written Exam Amazon(Bangalore)
- Nitin Gupta May 12, 2012 in India
Given a number in the form of string. Output the binary equivalent of that number.
Sample Input: "8.5"
Sample Output: 1000.1
Sample Input: "12.34.23"
Sample Output: "ERROR"| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm C C# C++ Coding Java Math & Computation - 0of 0 votes
Answershow does Java do Garbage Collection?
- Itcecsa May 11, 2012 in United States| Report Duplicate | Flag | PURGE
Morgan Stanley Financial Software Developer Java - 0of 0 votes
AnswersWhat is the contract between compareTo() and equals()
- shine May 09, 2012 in India| Report Duplicate | Flag | PURGE
Java - 0of 0 votes
AnswersIs singleton a class level OR JVM level implementation?
- Avi May 01, 2012 in India| Report Duplicate | Flag | PURGE
Accenture Java - 0of 0 votes
AnswersIs singleton a class level OR JVM level implementation?
- Avi May 01, 2012 in India| Report Duplicate | Flag | PURGE
Accenture Java - 0of 0 votes
AnswersIf my hashcode() method returns zero will it compile, run or throw error?
- Avi May 01, 2012 in India
Is it good practice for hashcode() method returns zero?| Report Duplicate | Flag | PURGE
Accenture Java - 0of 0 votes
AnswersWhat happens if you make a field both final and volatile?
- irraju April 29, 2012 in India| Report Duplicate | Flag | PURGE
Adobe Applications Developer Java