Interview Question for Java Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 2 vote

JAVA

L1.removeAll (L2);   and L2.removeAll(L1)

- lakshaman January 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 votes

I doesn't make sense. If you remove all L2's elements from L1 in first operation then

L2.removeAll(L1)

does nothing. To make it work you have to store original L1 in additional variable which I believe is bad solution considering memory constraints and objects size.

- thelineofcode January 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a hash table (if the numbers < N, and N is small use an array). Scan both list, if the number is already in the hash table, remove the number, else add the number in the hash table.

- Jason January 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesn't work. For example, if the number 2 appears 3 times, you'll add, remove, then add again. Basically, your solution will return all numbers that appear an odd number of times in the two lists.

- Anonymous January 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create hashMap and scan both list, add number as key and if it seen again add value +1

- sudBisht January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
public class Test {
	public static ArrayList a1 = new ArrayList();
	public static ArrayList a2 = new ArrayList();
	public static ArrayList a3 = new ArrayList();
	public static ArrayList check(ArrayList arf,ArrayList arl) 
	{
		for(Object i:arf)
		{
			if(!arl.contains(i))
			{
				a3.add(i);
			}
		}
		return a3;
	}
	public static void main(String[] args) {
				
		a1.add(1);a1.add(2);a1.add(3);a1.add(4);a1.add(5);
		a2.add(2);a2.add(2);a2.add(3);a2.add(4);a2.add(5);a2.add(6);
		check(a1,a2);
		check(a2,a1);
		System.out.println(a3);
	}

}

- thackoor February 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As sudBisht said.. here's the implementation.

public class NonRepeating {

    public static void main(String[] args) {
        List<Integer> a = Lists.newArrayList(1,1,1,13,4,6,77,85,43,34,3,3,77,13);
        List<Integer> b = Lists.newArrayList(1,1,1,13,41,6,56,85,43,34,3,3,77,13);

        Map<Integer, Integer> map = Maps.newHashMap();
        prepareMap(a, map);
        prepareMap(b, map);

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if(entry.getValue() == 1) {
                System.out.println(entry.getKey());
            }
        }
    }

    private static void prepareMap(List<Integer> a, Map<Integer, Integer> map) {
        for (Integer i : a) {
          Integer frequency = map.get(i);
          if( frequency == null ) {
             map.put(i, 1);
          }
          else {
              map.put(i, frequency+1);
          }
        }
    }
}

- Neha February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

creat a arrary of integer range ; so your space complexity will be constant and then
time complexity shoul be O(n);

public void printUncommon (List<Integer >L1 ,List<Integer>L2)
{
int[] numInts = new int[Integer.MAX_VALUE];
for(int i=0;i<Integer.MAX_VALUE;i++) numInts =0;
for(int i=0;i<L1.size();i++)numInts[L1.get(i)]++;
for(int i=0;i<L2.size();i++)numInts[L1.get(i)]++;
for(int i=0;i<Integer.MAX_VALUE;i++)
{
if(numInts[i] == 0) System.out.println(i);
}

- Danish Shaikh (danishshaikh556@gmail.com) February 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More