Student Interview Question for SDE1s


Country: India




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

#include<iostream>
using namespace std;

int main()
{
	int array[13] = {4,3,2,1,6,7,4,1,2,4,3,2,1};
	int hashtable[20] = {0};
	
	for(int i = 0; i < 13; i++)
	{
		int index = (int)(array[i]);
		hashtable[index] = hashtable[index] + 1;
		
	}

    for(int j = 0; j < 20; j++)
	{
		if(hashtable[j] == 1)
		{
			cout<<array[j]<<" ";
		}
		
	}
  	
	return 0;
}

output-> 6,7

- Monali April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the correct algorithm overall. Just one nitpick. As you probably know, you may not know the range of numbers passed in to create the hashtable with. It may be helpful to have a preliminary run through of the array to find the max element so that you can more accurately instantiate your hashtable to the right size.

- SK April 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Best way is to use a hashing function that ignores collission...
Reconvert using key-pair reversal and print result

- TrippingWeedGuy April 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Best way to solve this question is to use hashtable...

/Given an integer array with multiple repeated elements, print all distinct elements in array.

#include<iostream>
using namespace std;

int main()
{
int array[13] = {4,3,2,1,6,4,1,2,4,3,2,7,1};
int hashtable[20] = {0};

for(int i = 0; i < 13; i++)
{
int index = (int)(array[i]);
hashtable[index] = hashtable[index] + 1;

}

for(int j = 0; j < 20; j++)
{
if(hashtable[j] == 1)
{
cout<<array[j]<<" ";
}

}

return 0;
}

Output-> 6,7

- Monali April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;


public class RemovedDuplicateDataArray {

/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
Integer arr[]=new Integer[10];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < arr.length; i++) {
System.out.println("enter element --");
arr[i]=Integer.parseInt(br.readLine());
}
//put in arralist
List<Integer> list=new ArrayList<Integer>(Arrays.asList(arr));
//put in HashSet to sort data
HashSet<Integer> hashset=new HashSet<Integer>(list);
System.out.println(list+" "+hashset);
//again put in list
list=new ArrayList<Integer>(hashset);

//again put in array final
arr=new Integer[list.size()];
list.toArray(arr);

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);
}
}

}

- Anonymous April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;


public class RemovedDuplicateDataArray {

/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
Integer arr[]=new Integer[10];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < arr.length; i++) {
System.out.println("enter element --");
arr[i]=Integer.parseInt(br.readLine());
}
//put in arralist
List<Integer> list=new ArrayList<Integer>(Arrays.asList(arr));
//put in HashSet to sort data
HashSet<Integer> hashset=new HashSet<Integer>(list);
System.out.println(list+" "+hashset);
//again put in list
list=new ArrayList<Integer>(hashset);

//again put in array final
arr=new Integer[list.size()];
list.toArray(arr);

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);
}
}

}

- removeDulicate April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class multiple{
	public static void main(String[] args){
		int[] input = {1,2,3,4,5,5,7,8,8,0,0,12};
//		printUnique(input);
		printUniqueSet(input);
	}
	/*
	*	ArrayList solution
	*/
	static void printUnique(int[] input){
		boolean[] check = new boolean[256];
		ArrayList<Integer> output = new ArrayList<>();
		for(int i=0; i < input.length; i++){
			if(!check[input[i]]){
				output.add(input[i]);
			}
			check[input[i]] = true;	
		}
		System.out.println(output.toString());
	}

	/*
	*	LinkedSet Solution
	*/

	static void printUniqueSet(int[] input){
		Set<Integer> set = new LinkedHashSet<>();
		for (int x : input){
			set.add(x);
		}
		System.out.println(set.toString());
	}
	
}

- Anon April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class multiple{
	public static void main(String[] args){
		int[] input = {1,2,3,4,5,5,7,8,8,0,0,12};
//		printUnique(input);
		printUniqueSet(input);
	}
	/*
	*	ArrayList solution
	*/
	static void printUnique(int[] input){
		boolean[] check = new boolean[256];
		ArrayList<Integer> output = new ArrayList<>();
		for(int i=0; i < input.length; i++){
			if(!check[input[i]]){
				output.add(input[i]);
			}
			check[input[i]] = true;	
		}
		System.out.println(output.toString());
	}

	/*
	*	LinkedSet Solution
	*/

	static void printUniqueSet(int[] input){
		Set<Integer> set = new LinkedHashSet<>();
		for (int x : input){
			set.add(x);
		}
		System.out.println(set.toString());
	}
	
}

- ANON April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution seems great. Could you explain to me a little about the check part? Im a beginner.

- mjj April 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class multiple{
	public static void main(String[] args){
		int[] input = {1,2,3,4,5,5,7,8,8,0,0,12};
//		printUnique(input);
		printUniqueSet(input);
	}
	/*
	*	ArrayList solution
	*/
	static void printUnique(int[] input){
		boolean[] check = new boolean[256];
		ArrayList<Integer> output = new ArrayList<>();
		for(int i=0; i < input.length; i++){
			if(!check[input[i]]){
				output.add(input[i]);
			}
			check[input[i]] = true;	
		}
		System.out.println(output.toString());
	}

	/*
	*	LinkedSet Solution
	*/

	static void printUniqueSet(int[] input){
		Set<Integer> set = new LinkedHashSet<>();
		for (int x : input){
			set.add(x);
		}
		System.out.println(set.toString());
	}
	
}

- S April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void distinctElements(int iArr[n])
{
	int iDistinctEle[n];
	std::fill(iDistinctEle,iDistinctEle+n,-1);
	for(int i=0;i<n;i++)
	{
		if(iDistinctEle[i] !=-1)
		{
			if(iArr[i] ==iDistinctEle[i])
				continue;
		}
		iDistinctEle[iArr[i]] = iArr[i];
	}
	for(int j=0;j<n;j++)
	{
		if(iDistinctEle[iDistinctEle[j]] ==j)
			cout << iDistinctEle[j] <<' ';
	}
	return;
}

- sv April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how I write it in Java which would be very simple'

public static Integer[] unique(Integer[] input){
    Set<Integer> mySet= new HashSet<>(input.length);
    for(Integer i:input)
        mySet.add(i);
    return mySet.toArray(new Integer[1]);
        
}

The trick of question is for C for not using hashtable:

int* unique(int* input, int length){
    int max=0x7FFFFFFF+1;//minimum integer
    int min=0x7FFFFFFF;//maximum integer
    int i;
    
    //find the max and min of the array O(n)
    for(i=0;i<length;i++){
        if(max<input[i])
            max=input[i];
        if(min>input[i])
            min=input[i];
    }
    char* countArr = calloc(max-min+1,sizeof(char));
    int count=0;
    for(i=0;i<length;i++){
        countArr[input[i]-min]=1;
        count++;
    }
    int* output= calloc(count, sizeof(input[0]));
    int index;
    index=0;
    for(i=min;i<=max;i++)
        if(countArr[i-min]){
            output[index++]=i;
        }
    return output;        
}

I have to return the size of return array somehow that I did not do here.

- amirtar April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.LinkedHashSet;



public class Test {

public static void main(String args[]) {

int arr[] = {4,3,2,1,6,7,4,1,2,4,3,2,1};
HashSet<Integer> hs = new HashSet<Integer>();
for(int i=0;i<arr.length;i++) {
hs.add(arr[i]);
}
System.out.println(hs);
LinkedHashSet<Integer> hs1 = new LinkedHashSet<Integer>();
for(int i=0;i<arr.length;i++) {
hs1.add(arr[i]);
}
System.out.println(hs1);

}
}

- ankit jain April 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.LinkedHashSet;



public class Test {

public static void main(String args[]) {

int arr[] = {4,3,2,1,6,7,4,1,2,4,3,2,1};
HashSet<Integer> hs = new HashSet<Integer>();
for(int i=0;i<arr.length;i++) {
hs.add(arr[i]);
}
System.out.println(hs);
LinkedHashSet<Integer> hs1 = new LinkedHashSet<Integer>();
for(int i=0;i<arr.length;i++) {
hs1.add(arr[i]);
}
System.out.println(hs1);

}
}

- ankit jain April 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package test;

public class FirstElement {

public static void main(String[] args) {
Integer a[] = {2,5,5,6,2,7,6,7,1,10};
int k=0;
Integer arr[][] = new Integer[1][a.length];
for(int i=0,j=0; i<a.length; ){

if(j<=k){
if(arr[0][j] != null){
if(arr[0][j] == a[i])
{

i++;
j=0;

}

else
j++;

}
else{

arr[0][j] = a[i];
k++;
i++;
j=0;

}

}

}

for(int h=0; h<k; h++){

System.out.print(arr[0][h]+" ");
}

}

}

- Anonymous April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package test;

public class FirstElement {

public static void main(String[] args) {
Integer a[] = {2,5,5,6,2,7,6,7,1,10};
int k=0;
Integer arr[][] = new Integer[1][a.length];
for(int i=0,j=0; i<a.length; ){

if(j<=k){
if(arr[0][j] != null){
if(arr[0][j] == a[i])
{

i++;
j=0;

}

else
j++;

}
else{

arr[0][j] = a[i];
k++;
i++;
j=0;

}

}

}

for(int h=0; h<k; h++){

System.out.print(arr[0][h]+" ");
}

}

}

- Piyush goyal April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Add array to a set and return keys from set.

- Vib April 13, 2015 | 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