Interview Question for Android Engineers


Country: India
Interview Type: Phone Interview




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

// ZoomBA
symmetric_diff = list1 ^ list2

- NoOne October 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.eb.corealgo;

import java.util.ArrayList;

public class FindUnCommon {

	
	public ArrayList<Integer> getUnCommon(ArrayList<Integer> list1,ArrayList<Integer> list2){
		
		int indexOfElement=-1;
		if(list1.size()==0){
			return list2;
		}
		if(list2.size()==0){
			return list1;
		}
		
		ArrayList<Integer> returnList=new ArrayList<Integer>();
		//if not find form both
		for(int i=0;i<list1.size();i++){
			
			if(!list2.contains(list1.get(i))){
				returnList.add(list1.get(i));
			}
			if(list2.contains(list1.get(i))){
				indexOfElement=list2.indexOf(list1.get(i));
				list2.remove(indexOfElement);
			}
				
		}
		returnList.addAll(list2);
		
		return returnList;
	}
	
	//test Application
	public static void main(String args[]){
		
		FindUnCommon un=new FindUnCommon();
		
		ArrayList<Integer> l1=new ArrayList<Integer>();
		
		l1.add(1);
		l1.add(2);
		l1.add(3);
		l1.add(1);
		l1.add(5);
		
		ArrayList<Integer>l2=new ArrayList<Integer>();
		
		l2.add(3);
		l2.add(4);
		l2.add(5);
		l2.add(6);
		l2.add(7);
		
		System.out.println(un.getUnCommon(l1, l2));
		
	}
	
}

- shashi kumar May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take both the lists as a string. Then take two frequency counters f1[10] and f2[10]. Initialise all the 10 elements of both the arrays to 0. Now traverse the two strings from left to right. Increment the counter like- f1[ a [ i ] - ' 0 ' ]++ and f2[ b[i] - '0']++. Now take a loop from 0 to 9 and check whether f1[i]==f2[i] or not. Time complexity- O(list size)*10.

- rishi_07 May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using hashmap
firstly, if we use list related api Running time will be O(mn). Because, contains() in list is O(n)
Secondly, if we use set related api it maintains only unique elements but not how many times it repeated
To solve these two things we use hash map. It contains unique elements and maintains as value how many times each element is repeated. Also, we are using linkedhashmap to preserve the order in which elements occurred
Importantly, add/contains/remove methods are O(1)

// Time : O(m+n), Space : O(max(m,n))

public static List<Integer> findUncommonElements(Integer a1[], Integer a2[]) {
		if (null == a1 && null == a2) {
			return null;
		}
		if (null == a1 || a1.length == 0)
			return Arrays.asList(a2);
		if (null == a2 || a2.length == 0)
			return Arrays.asList(a1);

		List<Integer> result = new ArrayList<>();
		Map<Integer, Integer> countMap = new LinkedHashMap<>();
		for (int i : a2) {
			countMap.compute(i, (key, value) -> {
				if (null == value)
					return 1;
				return value + 1;
			});
		}

		for (int i : a1) {
			if (countMap.containsKey(i)) {
				countMap.remove(i);
			} else {
				result.add(i);
			}
		}
		for (int key : countMap.keySet()) {
			for (int i = 0; i < countMap.get(key); i++) {
				result.add(key);
			}
		}
		return result;
	}

- Raj May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Use Hashing O(n)
2. Use Sorting O(nlogn)

- Tissa May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I/P :
List 1 : 1, 2, 3, 1, 5
List 2 : 3, 4, 5, 6, 7
O/P : 1, 1, 2, 6, 7
Shouldn't the output be 1,2,4,6,7

- Poojan Jhaveri May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace careercup
{
class Program
{
static void Main(string[] args)
{
List<int> l1 = new List<int>(){1,5,6,3,2};
List<int> l2 = new List<int>() { 1, 7, 2, 9, 5 };

IList<int> uniqueNumbers = new List<int>();

foreach (var item in l1)
{
if (!action.CheckIfElementIsPresent(item, l2)) { uniqueNumbers.Add(item);

Console.WriteLine(item);
}

}
foreach (var item in l2)
{
if (!action.CheckIfElementIsPresent(item, l1))
{
uniqueNumbers.Add(item);

Console.WriteLine(item);
}
}

Console.ReadLine();

}
}
class action
{


public static bool CheckIfElementIsPresent(int a, ICollection<int> collection)
{

return collection.Where(x => x == a).Any();
}
}

}

- Franklin vegas May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
{
static void Main(string[] args)
{
List<int> l1 = new List<int>(){1,5,6,3,2};
List<int> l2 = new List<int>() { 1, 7, 2, 9, 5 };

IList<int> uniqueNumbers = new List<int>();

foreach (var item in l1)
{
if (!action.CheckIfElementIsPresent(item, l2)) { uniqueNumbers.Add(item);

Console.WriteLine(item);
}

}
foreach (var item in l2)
{
if (!action.CheckIfElementIsPresent(item, l1))
{
uniqueNumbers.Add(item);

Console.WriteLine(item);
}
}

Console.ReadLine();

}
}
class action
{


public static bool CheckIfElementIsPresent(int a, ICollection<int> collection)
{

return collection.Where(x => x == a).Any();
}
}

- Franklin vegas May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace careercup
{
class Program
{
static void Main(string[] args)
{
List<int> l1 = new List<int>(){1,5,6,3,2};
List<int> l2 = new List<int>() { 1, 7, 2, 9, 5 };

IList<int> uniqueNumbers = new List<int>();

foreach (var item in l1)
{
if (!action.CheckIfElementIsPresent(item, l2)) { uniqueNumbers.Add(item);

Console.WriteLine(item);
}

}
foreach (var item in l2)
{
if (!action.CheckIfElementIsPresent(item, l1))
{
uniqueNumbers.Add(item);

Console.WriteLine(item);
}
}

Console.ReadLine();

}
}
class action
{


public static bool CheckIfElementIsPresent(int a, ICollection<int> collection)
{

return collection.Where(x => x == a).Any();
}
}

}

- Franklin vegas May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace careercup
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> l1 = new List<int>(){1,5,6,3,2};
            List<int> l2 = new List<int>() { 1, 7, 2, 9, 5 };

            IList<int> uniqueNumbers = new List<int>();

            foreach (var item in l1)
            {
                if (!action.CheckIfElementIsPresent(item, l2)) { uniqueNumbers.Add(item);

                Console.WriteLine(item);
                }
                
            }
            foreach (var item in l2)
            {
                if (!action.CheckIfElementIsPresent(item, l1))
                {
                    uniqueNumbers.Add(item);

                    Console.WriteLine(item);
                }
            }

            Console.ReadLine();

        }
    }
    class action
    {


        public static bool CheckIfElementIsPresent(int a, ICollection<int> collection)
        {

            return collection.Where(x => x == a).Any();
        }
    }

}

- Franklin May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is changing the original list allowed?
any constrain with time complexity

- Giri May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void solution(int[] input1, int[] input2)
{
HashMap<Integer,Integer> hm = HashMap<Integer,Integer>();
for(int i=0,int j=0;i<input1.length ||j<input2.length;i++;j++)
{
	

	if(i<input1.length && hm.containsKey(input1[i]))
	{
       		int count = hm.get(input1[i]);
		if(count == 0 || count >0)
		{
			// Do nothing, its either both or only A
		}
		else
		{
			// its there in both the lists
			hm.put(input1[i],0);
		}

        }
	else
		hm.put(input[i],1);

	
	if(j<input2.length && hm.containsKey(input2[j]))
	{
       		int count = hm.get(input2[i]);
		if(count == 0 || count <0)
		{
			// Do nothing, its either both or only B
		}
		else
		{
			// its there in both the lists
			hm.put(input2[i],0);
		}

        }
	else
		hm.put(input[i],-1);

	


}

Iterator it = hm.entrySet().iterator();

while(it.hasNext())
{
 	Map.Entry pair = (Map.Entry) it.next();
	if(it.getValue()!=0)
		System.out.println(it.getValue());

}

}

- skumaringuva May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;



public class UnCommonCharacters {

/**
* @param args
*/
public static void main(String[] args) {

String str1= "characters";
String str2= "alphabets";
char[] c1= str1.toLowerCase().toCharArray();
char[] c2= str2.toLowerCase().toCharArray();

Map<Character, Integer> countMap = new TreeMap<>();
for(char i : c1){
countMap.put(i, 1); // Present in 1st String
}

for(char i : c2){
if(countMap.containsKey(i)){
countMap.put(i,-1); // Common to both the strings
}
else {
countMap.put(i,2); // Present in 2nd String
}
}

for(Entry<Character, Integer> ent : countMap.entrySet())
if(!(ent.getValue() == -1)){
System.out.println(ent.getKey());
}

}

}

- Anonymous July 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Python
def find_uncommon(list1, list2):

uncommon_elements = []

for item in list1:
if item not in list2:
uncommon_elements.append(item)
for item in list2:
if item not in list1:
uncommon_elements.append(item)

return uncommon_elements

find_uncommon([1, 2, 3, 1, 5], [3, 4, 5, 6, 7])

- eduardo.bal699 April 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Python
def find_uncommon(list1, list2):

    uncommon_elements = []

    for item in list1:
        if item not in list2:
            uncommon_elements.append(item)
    for item in list2:
        if item not in list1:
            uncommon_elements.append(item)    
    
    return uncommon_elements

find_uncommon([1, 2, 3, 1, 5], [3, 4, 5, 6, 7])

- eduardo.bal699 April 27, 2018 | Flag Reply


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