Java developer Interview Questions in United States
java developer interview questions shared by candidates
Top Interview Questions
Lead Java Developer at EPAM was asked...
Coding test: 1. Given a string, find out if there's repeat characters in it. 2. SQL. Given a Customer table and a Payment table (with Customer ID as primary and foreign key), write a query to output a list of customers who have not paid their bills in the last 3 months. 17 AnswersDid well on the first Java coding test. Solution using Hashset. Not so well on the SQL query. Able to give a query, but a few errors in it I am sure. Also there was a second questions regarding the SQL query, which we didn't have time to get to. So that was another bad thing. I am pretty sure I did not pass. But good learning experience. import java.io.*; public class CountChar { public static void main(String[] args) throws IOException { String ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the Statement:"); ch=br.readLine(); int count=0,len=0; do { try { char name[]=ch.toCharArray(); len=name.length; count=0; for(int j=0;j=65&&name[0]=97&&name[0]<=123))) count++; } if(count!=0) System.out.println(name[0]+" "+count+" Times"); ch=ch.replace(""+name[0],""); } catch(Exception ex){} } while(len!=1); } } package epam; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class RepeatedCharactersInString { public static void main(String[] args) { ArrayList arrli = new ArrayList(); Scanner sc = new Scanner(System.in); String s = sc.next(); String s1=""; for (int i = 0; i < s.length(); i++) { arrli.add(s.charAt(i)); } for (int i = 0; i < arrli.size(); i++) { for (int j = i + 1; j < arrli.size(); j++) { if (arrli.get(i) == arrli.get(j)) { arrli.remove(j); j--; } } } for (int i = 0; i < arrli.size(); i++) { // System.out.println(arrli.get(i)); s1=s1+arrli.get(i); } System.out.println(s1); sc.close(); } } Show more responses SQL> select customername from customers inner join Payment on customers.customer id=Payment.customerid where payment.due>=3 package epam; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class RepeatedCharactersInString { public static void main(String[] args) { ArrayList arrli = new ArrayList(); ArrayList arrli2 = new ArrayList(); Scanner sc = new Scanner(System.in); String s = sc.next(); String s1=""; for (int i = 0; i < s.length(); i++) { arrli.add(s.charAt(i)); } for(int i=0;i package epam; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class RepeatedCharactersInString { public static void main(String[] args) { ArrayList arrli = new ArrayList(); ArrayList arrli2 = new ArrayList(); Scanner sc = new Scanner(System.in); String s = sc.next(); String s1=""; for (int i = 0; i < s.length(); i++) { arrli.add(s.charAt(i)); } for(int i=0;i public class Test { public static void main(String args[]) { String test = "abcddeff"; ArrayList listStr = new ArrayList(); for (int i =0; i< test.length(); i++) { if (listStr.contains(test.charAt(i))) { System.out.println("Repeated Character Found:"+test.charAt(i)); } else { listStr.add(test.charAt(i)); } } } } ----To find out repeated characters in a given string import java.util.*; public class test { public static void main(String[] args) { String str = "abdc"; char[] arr = str.toCharArray(); HashSet set = new HashSet(); for (char i : arr ) { set.add(i); } if((set.size()) == (arr.length)) System.out.println("unique character"); else System.out.println("repetition"); } } Solution 1: String str="asaaatttmmppo"; HashMap map = new HashMap(); for(char c : str.toCharArray()) { if(map.containsKey(String.valueOf(c))) { map.put(String.valueOf(c), map.get(String.valueOf(c))+1); }else { map.put(String.valueOf(c),1); } } map.forEach((x,y)-> { if(y>1) { System.out.println(x); } }); Solution 2: String str="asdfghjkloiuyt"; HashSet c = new HashSet(); for(char ch:str.toCharArray()) { c.add(ch); } System.out.println(c.size() != str.length()); import java.util.*; public class FindDuplicates{ public static void main(String []args){ String test = "abccdefga"; char[] arr = test.toCharArray(); List list = new ArrayList(); for(int i=0; i SQL: select a.customer_name from Customer a left join Payments p on p.cid =a.cid where DATEDIFF(day,p.lastPaymentDate,getdate())>90; String res=""; for(int i=0;i No Show more responses No Coding test: 1. Given a string, find out if there's repeat characters in it. public static void findRepeatedCharacters(String input) { String normalized = (input.toLowerCase()).trim(); char[] inputCharArray = normalized.toCharArray(); int index = 1; String searchStr = ""; String repatedChars = ""; for (char c : inputCharArray) { searchStr = normalized.substring(index); if (searchStr.contains(Character.toString(c))) { if (!repatedChars.contains(Character.toString(c))) { repatedChars = repatedChars + Character.toString(c); } } index = index + 1; } if (repatedChars.length() > 0) { System.out.println("Repeated characters in the input string '" + normalized + "' are : " + repatedChars); } else { System.out.println("There are no repated characters in the input string '" + normalized); } } Given a Customer table and a Payment table (with Customer ID as primary and foreign key), write a query to output a list of customers who have not paid their bills in the last 3 months. Solution : Select * from customer where customer.customerid NOT IN (SELECT payment.customerid FROM payment where paymentdate >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)); One or more comments have been removed. |
Java Developer at Systems Logic was asked...
They asked me about the Java Object - Oriented, Inheritance and Out-put the single value of the 2 dimension array 7 AnswersAll the answers can be easily found from internet/ java book if you repair them before Could you be more specific about the questions? And could you explain how you answered their questions, please! I'm gonna have the C# interview from Systems Logic, and they're gonna ask me questions about C++. So, if you can specify the questions they asked you, it could be more helpful to me. Please!!! I have got the phone all and email from them but that's it. Did they contact you again? How did you set up the appointment with them? Show more responses Hi all. I've just passed the 2nd round technical interview for the JAVA Developer position. They sent me an email about attending a training session at their head office before job placement. Did any of you guys have such experience? What kind of question did they ask you in the technical interview? I have passed the technical interview and I received an offer letter and two agreements (training agreement and employment agreement) from them. But a lot of things in the agreements are not very clear, it didn't say anything about paid holidays, employee insurances, etc. I am not sure if I should sign it or not. Does anyone have idea about this whole thing? can you please tell me if this good company? |
Java Programmer at Barclays was asked...
Assume there is a method provided getNextperson() which gives Person objects which have comparable interface implemented, now read from a file records and sort it and give first 1000 records, write code on the paper 7 Answersmaybe use tree data structure tree data structure ? i hope u meant to say "TreeSet"; but the question doesnt say that getNextPerson() will be unique. So let us not bank on treeSet. So just add them on to a arrayList and do a Collections.sort(arrayList); The question is actually quite simple since you were given that Comparable interface is already implemented (eg, public int compareTo(Object o) method is overridden in Person object). First step, 'read from a file all records' and store each Person object returned by getNextPerson() in List records = new ArrayList(). Next step, call Collections.sort(records) to sort (by criteria specified in compareTo() method, such as Last and First name, or any other meaningful parameter). Final step, 'give first 1000 records' -- either by iterating over first 1000 (checking size() >= 1000) or by returning the list consisting of only 1000 records (either copy into a new list with size 1000 or by using removeRange method in ArrayList. Show more responses How would you read a 2GB file and put in a arraylist and then sort it? assuming memory constraint Use a heap based data structure(min or max) to keep replacing the root once you have a 1000 records. java priority queue. Using heap is the rightapproach. Array list approach wont work if the file is so huge. Implementing heap using array to store the top 1000 records. (note: since the person implements comparable, we just have to compare the new person with root node i.e index 0 in heap. if root is already greater than new person, just skip the new person else add it as root and call heapify). Finally we can call the Arrays.sort on heap and return the result. One or more comments have been removed. |
Java Software Engineer at Google was asked...
Given two files that has list of words (one per line), write a program to show the intersection. 6 AnswersRead into a list, sort. For two loops, iterate and advance each index as appropriate. The problem itself is not that hard but what makes it difficult for me was trying to do it on the white board. My advice: have some practice run yourself on a white board with some of the fundamental algo questions: the kinds that will find in CS 101 mid-term. Better still to read the first into a HashMap and then as you read the second list look it up in the HashMap. If you find the entry in the HashMap put it into a List containing the intersections. For any reasonable hashing function that would be O(n) for the whole operation. Of coure, since we're talking about reading from disk files it will all probably be I/O bound. Hashmap is good. How about a hashset? the checking of duplicate elements in the same file is taken care automatically; also while creating the intersection list, "contains" is taken care automatically; also same efficiency as a hashmap !! Show more responses The best soln imo is to use a trie. So put the list of first words into a trie. And then go through the list of second words and check if they are in the trie. Would be order of n , for inserting and checking , where n is length of the word. Memory can ,in worst case can be 26 power n, assuming english alphabet. Cmcr This problem is the textbook example for a bloom filter (because of large data and likelihood of disk access). This is probably the insight they were hoping for, but they probably would have settled for using a common hash set as well. |
public class Person { Person father; Person mother; Gender gender; Integer age; List /** * Returns the Persons which are the greatest number of levels up the family tree * from this instance. Examples: * Given a tree where this person instance only has a mother and father, return mother and father. * Given a tree where this person instance has a mother & father, and a grandfather on the mother's side, return only the grandfather on the mother's side. * @return List of Person */ public List getGreatestAncestors() { // If this is the root of the tree, return empty array because there is no ancestor for the tree if (this.father == null && this.mother == null) { return new ArrayList(); } List fList = new ArrayList(); List mList = new ArrayList(); if (this.father != null) { fList = this.father.getGreatestAncestors(); } if (this.mother != null) { mList = this.mother.getGreatestAncestors(); } List results = new ArrayList(); for (Person p : fList) { if (results.contains(p)){ continue; } results.add(p); } for (Person p : mList) { if (results.contains(p)){ continue; } results.add(p); } return results; } What do they expect in onsite interview for senior software engineer for app development I cranked this out in about 30 minutes. I believe it works quite well. I've also included the corresponding unit tests: file: Person.java ------------------------------------------------------------------------------------------------------------- package Command; import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class Person { Person father; Person mother; Gender gender; Integer age; List children; int level = 0; public enum Gender { Male, Female; } Person(Person dad, Person mom, Gender gender, int age, int level) { this.father = dad; this.mother = mom; this.gender = gender; this.age = age; this.level = level; } public void setChildren(List children) { this.children = children; } public void addChild(Person child) { this.children.add(child); } public List getOldestSisters () { // given the current person (self), determine parents // then get children of those parents // Determine gender of each child // Where they are female, get ages // return females with age > mine // Note: must check on both sides father/mother as there may be a mixed marriage // Combine list of children - Exclude YOU as you cannot be your own sister. // Use a set to eliminate duplicates. HashSet allChildren = new HashSet(); // Can't add null to a hashSet so screen for it. if ((father != null) && (father.children != null)){ allChildren.addAll(father.children); } if ((mother != null) && (mother.children != null)) { allChildren.addAll(mother.children); } // If you are not in this list, there is an issue! if (allChildren.contains(this)) { allChildren.remove(this); // System.out.println("Removing self from list."); } else { System.out.println("Error: You are not a child of your parents! Adopted?"); } // Filter down to only women and get any older than me: int myAge = this.age; List oldestSisters = new ArrayList(); for (Person child : allChildren) { if (child.gender == Gender.Female) { if (child.age > myAge) { oldestSisters.add(child); } } } return oldestSisters; } public List getGreatestAncestors() { if ((this.father == null) || (this.mother == null)) { return null; // You must have two parents to have ancestors } // Find root parents List myParents = getParents(this); return getElders(myParents); } private List getElders(List parents) { List elders = new ArrayList(); List myParents = new ArrayList(); boolean newElders = false; for (Person parent : parents) { myParents = getParents(parent); if (myParents.isEmpty()) { elders.add(parent); } else { elders.addAll(myParents); newElders = true; } } if (newElders == true) { return getElders(elders); } return elders; } private List getParents(Person person) { List parents = new ArrayList(); if (person.father != null) parents.add(person.father); if (person.mother != null) parents.add(person.mother); return parents; } } // For the above class, you basically have to implement 2 methods. // public List getOldestSisters() // public List getGreatestAncestors() Show more responses package Command; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; class PersonTest { // Create grand parents Person grandFatherDad = new Person(null, null, Person.Gender.Male, 78, 1); Person grandMotherDad = new Person(null, null, Person.Gender.Female, 81, 1); Person grandFatherMom = new Person(null, null, Person.Gender.Male, 69,1); Person grandMotherMom = new Person(null, null, Person.Gender.Female, 89, 1); // Create parents / aunts and uncles Person father = new Person(grandFatherDad, grandMotherDad, Person.Gender.Male, 48, 2); Person mother = new Person(grandFatherMom, grandMotherMom, Person.Gender.Female, 47, 2); Person aunt1 = new Person(grandFatherMom, grandMotherMom, Person.Gender.Female, 47, 2); // Twin sis to mom Person aunt2 = new Person(grandFatherDad, grandMotherDad, Person.Gender.Female, 37, 2); Person uncle1= new Person(grandFatherDad, grandMotherDad, Person.Gender.Male, 35, 2); // Children (me and bros and sis) Person self = new Person(father, mother, Person.Gender.Male, 18, 3); Person brother1 = new Person(father, mother, Person.Gender.Male, 16, 3); Person sister1 = new Person(father, mother, Person.Gender.Female, 15, 3); Person sister2 = new Person(father, mother, Person.Gender.Female, 14, 3); @BeforeEach void setUp() { // Create children / sibling groups List children = new ArrayList(); children.add(father); children.add(aunt1); children.add(uncle1); grandFatherDad.setChildren(children); grandMotherDad.setChildren(children); children.clear(); children.add(mother); children.add(aunt2); grandFatherMom.setChildren(children); grandMotherMom.setChildren(children); children.clear(); children.add(self); children.add(brother1); // Dad had brother with other wife children.add(sister2); father.setChildren(children); // mom came with her own daughter from prior marriage children.clear(); children.add(self); children.add(sister1); children.add(sister2); mother.setChildren(children); } @AfterEach void tearDown() { } @Test void getOldestSisters() { // When there are no older sisters and I am male: //Person me = new Person(father, mother, Person.Gender.Male, 48); List olderSisters = null; olderSisters = self.getOldestSisters(); assertTrue(olderSisters.isEmpty(), "No older sisters."); // When there is one older sister and I am male Person sister3 = new Person(father, mother, Person.Gender.Female, 50, 3); mother.addChild(sister3); father.addChild(sister3); olderSisters = self.getOldestSisters(); assertTrue(olderSisters.contains(sister3)); assertTrue(olderSisters.size() == 1, "One older full sister."); // Youngest Sister has two older sisters (one full, one half) olderSisters.clear(); olderSisters = sister2.getOldestSisters(); assertTrue(olderSisters.contains(sister1)); assertTrue(olderSisters.contains(sister3)); assertTrue(olderSisters.size() == 2, "One older full, one older half"); } @Test void getGreatestAncestors() { List ancestors = self.getGreatestAncestors(); assertTrue(ancestors.size() == 4); assert(ancestors.contains(grandFatherDad)); assert(ancestors.contains(grandFatherMom)); assert(ancestors.contains(grandMotherDad)); assert(ancestors.contains(grandMotherMom)); ancestors = father.getGreatestAncestors(); assertTrue(ancestors.size() == 2); assert(ancestors.contains(grandFatherDad)); assert(ancestors.contains(grandMotherDad)); ancestors = mother.getGreatestAncestors(); assertTrue(ancestors.size() == 2); assert(ancestors.contains(grandFatherMom)); assert(ancestors.contains(grandMotherMom)); ancestors = grandFatherDad.getGreatestAncestors(); assertNull(ancestors, "getGreatestAncestors():Persons with no ancestors should return null."); } } public class Person { public static int maxLevel = 0; public static int maxSisterAge = 0; Person father; Person mother; Gender gender; Integer age; List children; public enum Gender { Male, Female } public Person(Gender gender, Integer age) { this.gender = gender; this.age = age; } /** * Returns a list of Person objects which represent the oldest sisters of this Person instance. * A sister of a Person is someone who shares at least one parent, but not necessarily both parents. * The reason the return value is a List instead of a single * Person instance is because it's possible that there are multiple female siblings that are * the same age (twins, oldest daughter and mother's and father's side same age, etc...) * There will be no duplicate Person objects. * The returned list should not include this Person *x * @return List of Person or empty List if there are no sisters */ public List getOldestSisters() { List list = new ArrayList(); if (this.mother != null) { getMaxAge(this.mother); } if (this.father != null) { getMaxAge(this.father); } if (this.mother != null) { //get all female children from mother side getOldestFemaleChildren(this.mother, list); } if (this.father != null) { // get all female children from father side getOldestFemaleChildren(this.father, list); } return list; } private void getMaxAge(Person p) { for (Person child : p.children) { if (!child.equals(this) && child.gender == Gender.Female && child.age > maxSisterAge) { maxSisterAge = child.age; } } } private void getOldestFemaleChildren(Person root, List list) { for (Person child : root.children) { if (!child.equals(this) && child.gender == Gender.Female && child.age == maxSisterAge && !list.contains(child)) { list.add(child); } } } @Override public boolean equals(Object that) { if (this == that) { return true; } if (!that.getClass().getName().equals(this.getClass().getName())) { return false; } Person p = (Person) that; return p.mother.equals(this.mother) && p.father.equals(this.father) && p.age.equals(this.age) && p.gender == this.gender; } /** * Returns the Persons which are the greatest number of levels up the family tree * from this instance. Examples: * Given a tree where this person instance only has a mother and father, return mother and father. * Given a tree where this person instance has a mother & father, and a grandfather on the mother's side, * return only the grandfather on the mother's side. * * @return List of Person */ public List getGreatestAncestors() { List list = new ArrayList(); getMaxLevel(0); getGreatestAncestors(list, 0); return list; } private void getMaxLevel(int level) { // check if this node is the root of the tree if (this.father == null && this.mother == null) { if (maxLevel list, int level) { // base case if (level == maxLevel) { list.add(this); return; } if (this.mother != null) { this.mother.getGreatestAncestors(list, level + 1); } if (this.father != null) { this.father.getGreatestAncestors(list, level + 1); } } } |
Java Developer at EPAM was asked...
What are Exceptions in Java. 7 AnswersProgramming structure which can interrupt normal program execution stack and deliver Exception message to the objects are down the execution stack. You can create different types of exceptions and treat them differently in the catching block. Exception is an error event that can happen at the run time of a program. Exception or similar to error which are occurred at the time of run time. Exception can be handled but errors can not be handled. -> Exceptions are occurred when ever we encounter the case that may cause error. Show more responses An exception is an event that disrupts the normal flow of the program. Really good lessons to get into top tech companies: bit.ly/faang100 One or more comments have been removed. |
Java Developer Trainee at Naaptol was asked...
what will system.out print do? 4 AnswersI agree with you...this company cannot be trusted.they don't know how to take interviews.these people will never appreciate your knowledge. If you have reference than you will be treated very well. Even if you don't have technical knowledge 3 months 4 rounds of interview just shows how screwed up their hiring process is. Show more responses I was interviewed 2 time but never got jobs |
A rustic village contains one million married couples and no children. Each couple has exactly one child per year. Each couple wants a girl, but also wants to minimize the number of children they have, so they will continue to have children until they have their first girl. Assume that children are equally likely to be born male or female. Let p(t) be the percentage of children that are female at the end of year t. What is p(t)? "Can't tell" is a potential answer if you don't have sufficient information. 6 Answerscan't tell The answer is 2^-t 100(1-2^-t) Show more responses p(t) = 50% at all years (with some rounding) P(1) = 1,000,000 children, 500,000 of them girls, 500,000 of them boys P(2) = 500,000 more children (only those who had boys have another child), 250,000 girls, 250,000 boys so overall, there are now 750,000 girls, and 750,000 boys P(3) = 250,000 more children, 125,000 girls, 125,000 boys so there are now 875,000 girls (with 500,000 being only children, 250,000 having one brother, and 125,000 having 2 brothers), and 875,000 boys So, except for some rounding when the previous year's number of all boy families is an odd number, you will always have 50% of each One or more comments have been removed. |
Java Developer at Revature was asked...
What is inheritance? 4 AnswersThe ability of a new class to be created, from an existing class by extending it, is called inheritance. Inheritance is a mechanism where, in a new class is derived from an existing class. When a child class inherits the characteristics of the parents class Show more responses Inheritance is a state where the properties of an existing parent element is extended to children elements. |
Java Developer at eClinicalWorks was asked...
On the order of execution of Java code blocks when a class extends another class 4 AnswersCould you please expand? This is the case of inheritance. Where we get all the methods of extended class to extending class When a class extends another class, the execution of code is from parent to child. Firstly it calls the parent class methods/constructor after that child's class. Show more responses When one class have Methods which we have to use in another class that time extend one class to other. |