Interview Question


Team: Algorthims
Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

/**
 * 
 */


import java.util.ArrayList;
import java.util.List;

/**
 * @author singhnirajara
 *
 */
public class SameElement {

	/**
	 * @param args
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		List x = new ArrayList();
		List y = new ArrayList();
		x.add(1L);
		x.add("Niraj");
		x.add(123.52f);
		x.add(123);
		
		y.add(1L);
		y.add('C');
		y.add(123.52f);
		y.add(123d);
		 
		for(Object t:getSameObjects(x,y)){
			System.out.println(t);
		}
		
		
	}
	

	private static <T extends Object> List<T> getSameObjects(List<T> x, List<T> y){
		List<T> xy = new ArrayList<T>();
		for(T t:x){
				if(y.contains(t))xy.add(t);
		 }
			return xy;
	
	}

}

- Niraj Kumar Singh February 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using contains method we can get the same object from list 1

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

that would give you i think: O(nlogn)
is there any other better way?

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

public static <T> Collection <T> intersect (Collection <? extends T> a, Collection <? extends T> b)
	{
	    Collection <T> result = new ArrayList <T> ();

	    for (T t: a)
	    {
	        if (b.contains (t)) result.add (t);
	    }

	    return result;
	}

Use this code find the common elements.

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

Hash table can solve that in linear time.

- Alex February 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but it will require o(n) space for hashtable storage

- rishi singh March 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use retainAll method of collection

- sunny bindal February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use retainAll method of collection

- sunny bindal February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/// Collection<Object> collection = new ArrayList<Object>();
collection.add("A");
collection.add("B");
collection.add("C");
collection.add("D");

Collection<Object> collection2 = new ArrayList<Object>();
collection2.add("A");
collection2.add("B");
collection2.add("C");
collection2.add("E");

collection.retainAll(collection2);

for (Object str : collection) {
System.out.println(str);
} ///

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

Collection<Object> collection = new ArrayList<Object>();
		collection.add("A");
		collection.add("B");
		collection.add("C");
		collection.add("D");
		
		Collection<Object> collection2 = new ArrayList<Object>();
		collection2.add("A");
		collection2.add("B");
		collection2.add("C");
		collection2.add("E");
		
		collection.retainAll(collection2);
			
		for (Object str : collection) {
			System.out.println(str);
		}

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

Won't the solutions containing retainAll() or contains() have time complexity O(n^2). Why not use HashMap. Use a HashMap to store the first List and then while taking second List as input, just compare in HashMap whether element is already there or not.
Time complexity: O(n)

- nharryp August 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That can be done but it would require storage of the HashMap.

When i gave the answer of contains the interviewer asked once u gave me all the common elements in both the lists now give me no similar objects from both the lists.
some thing like A n ( A u B) , ( A u B) n B
the contains solution can handle the same question with a small tweak of removing /flagging the objects in either of the list.

- kmkswamy August 08, 2014 | Flag


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