Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Add the items to a set as you go and check to see if each item is already in the set. Every time you find a new item that isn't in the set, do array[numItemsFound] = array[currentIndex]; numItemsFound++; Complexity is O(n) and needs O(n) extra space. If you can't use extra space, an in-place sort might be the best bet, giving you O(n log n) or O(kn) if you use radix sort. If you can't use extra space AND you also can't re-order the original array, you're probably stuck checking every element with every element, O(n^2)

- eugene.yarovoi October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace HashTableDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();

            int [] arr = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 8, 6, 3, 5, 7, 3, 2 };

            int x;

            for (int i = 0; i < arr.Length; i++)
                if (ht.Contains(arr[i].ToString()))
                {
                    x = (int)ht[arr[i].ToString()];
                    ht[arr[i].ToString()] = ++x;
                }
                else
                    ht[arr[i].ToString()] = 1;

            foreach (DictionaryEntry e in ht)
                Console.WriteLine(e.Key);
        }
    }
}

- RAHUL

- Complete code for eliminating duplicates in an array... October 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if ur using .net... u would have used.. Linq

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

How about BST

- Anonymous November 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey74814" class="run-this">import java.util.*;
import java.util.HashSet;
import java.io.*;
import java.lang.*;

class RemoveDuplicated {
public static void main(String[] args){

RemoveDuplicated RD = new RemoveDuplicated();

String input = "thestringisduplicatedqwertyuio";
String output = RD.InsertToHashSet(input);
System.out.println(output);
}

public String InsertToHashSet(String input){

HashSet<String> CheckDuplicate = new HashSet<String>();
String output="";
int i,j;
j = 0;

for(i=0;i<input.length();i++)
{
String current = "";
current += input.charAt(i);

if(CheckDuplicate.contains(current)) //check if the char is already in the HashSet,if yes,do nothing
continue;
else { //if not,add current char to HashSet and put it in the output
CheckDuplicate.add(current);
output = output + current;
j++;
}
}

return output;
}

}
</pre><pre title="CodeMonkey74814" input="yes">
</pre>

- ozhang@usc.edu December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use C++ STL set and insert the elements in the array into the set. Now extract the elements from the set. (Set does not allow duplicate values).

- Sasi February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void removeDuplicates(char[] str)
{
if (str == null) return ;
int len = str.length;
if(len<2) return ;

int tail = 1;
for ( int i=1; i<len ; ++i)
{
int j;
for ( j = 0 ; j<tail ;++j)
{
if(str[i] == str[j])
break ;
}
if(j ==tail)
{
str[tail] = str[i];
++tail;
}
}
str[tail] =0;
}

- Andrew May 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package arrays;

/**
 * remove duplicate integers in array
 *  
 *
 */
public class RemoveDuplicatesInArray {

	public static void main(String [] args){
		int a[] = {1,1,1,1};
		
		int tail = 1;
		for (int i = 1;i<a.length;i++){
			int j =0;
			for(;j<tail;j++){
				if (a[j] == a[i]){
					break;
				}
			}
			if(j==tail){
				a[tail]= a[i];
				tail++;
			}			
		}
		
		for (int i=0;i<tail;i++) {
			System.out.print(a[i]);
		}
	}
}

- sriniatiisc October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I recently received this question in an early Google interview, so this is a pretty important question.

I did mine in Java with HashSet and this is what I got.

//O(2n) two passes, one for first for loop, second for toArray method
    public static Integer[] uniqueHashElement(Integer[] startArray)
    {
        //CAN'T DO Set<integer> set = new Set because it's abstract
        Set<Integer> set = new HashSet<Integer>();
        int numItemsFound = 0;

        for (int i = 0; i < startArray.length; i++)
        {
            set.add(startArray[i]);

        }
        Integer[] result = new Integer[set.size()];
        return set.toArray(result);

    }

- Anonymous November 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] removeDuplicates(int [] input){
		HashSet<Integer> H = new HashSet<Integer>();
		for (Integer integer : input) {
			System.out.println(integer);
			H.add(integer);
		}
		int result[] = new int[H.size()];
		int counter = 0;
		for(Integer x: H){
			result[counter++] = x;
		}
		return result;
	}

- albertchenyu March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Removeduplicatenumber {
public static void main(String args[])

{

int array[] = { 10, 20, 30, 20, 40, 40, 50, 60, 70, 80 };
int size = array.length;
System.out.println("Size before deletion: " + size);

for (int i = 0; i < size; i++)
{

for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
{
while (j < (size) - 1)
{
array[j] = array[j + 1];// shifting the values
j++;
}
size--;
}
}
}
System.out.println("Size After deletion: " + size);

for (int k = 0; k < size; k++)
{
System.out.println(array[k]); // printing the values
}
}}

- Suseela November 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Sort the array and remove duplicates by traversing it.
Complexity O(nlogn)

- anshul zunke October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use a hash table ppl !!!

HashTable ht = new HashTable();

for(int i=0 ; i<n ;i++)
    if(ht.contains(inputArray[i])
          ht[inputArray[i]]++;
    else
          ht[inputArray[i]] = 1;

// Display the elements
foreach(DictionaryEntry e in ht)
    Console.Writeline(e.Key);

- Rahul October 25, 2011 | 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