Amazon Interview Question for SDE1s


Country: India




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

Simply use a Hashtable

int array[]={6,4,2,9,1,8,7,3,5};
		HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
		int sum=10;
		
		for(int i=0;i<array.length;i++)
		{
			int diff=sum-array[i];
			if(map.containsKey(diff))
			{
				System.out.println(" "+array[i]+" -- "+diff);
			}
			map.put(i, array[i]);
		}

- Ashish July 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use hashtable or map

void readStream(void){
    unordered_map<int,int> hashtable;
    while(true){
        int num;
        cin >> num;
        int key = 10-num;
        cout << (hashtable.find(key) == hashtable.end()? false : true) << endl;
        hashtable[num] = 1;
    }
}

- swapnilsj July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. you could store the incoming integers in a heap and look for two numbers that sum to 10
2. read each incoming number and see if it's compliment (against 10 exists in a hash table)

- confused_coder July 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you are getting a steam of +ve integers and you have only a very few options
1. 0 + 10
2. 1 + 9
3. 2 + 8
...
11. 0 + 10

So you can use an array instead of heap.

- Vijay July 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you are getting a stream of only +ve numbers then you have only 11 options to get a sum of 10.
1. 0 + 10
2. 1 + 9
...
11. 10 + 0

So you can use an array or as confused coder posted you can use a hashtable.

- Vijay July 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Sumis10 {

public static void main(String[] args) {
// TODO Auto-generated method stu
int[] arr = new int[11];

for (int j = 1; j <= 10; j++) {
arr[j] = j;
}

int sum = 10;

HashMap<Integer, Integer> hm = new HashMap<>(arr.length);
for (int j = 0; j < arr.length; j++) {
hm.put(arr[j], sum - arr[j]);
}

Scanner sc = new Scanner(System.in);
String a = sc.next();
sc.close();
// System.out.println(a);
String[] numberarrstream = a.split(",");
ArrayList<Integer> differeneLeft = new ArrayList<>();
int streamNumber = 0;
boolean is_found = false;
for (String s : numberarrstream) {

streamNumber = Integer.parseInt(s);
for (Integer number : differeneLeft) {
if (streamNumber == number) {
is_found = true;
break;
}
}
if (is_found)
break;

if (hm.containsKey(streamNumber)) {
int differenceleft = hm.get(streamNumber);

differeneLeft.add(differenceleft);
}
}
System.out.println("First Combination of number 10 " + ">>>" + streamNumber);

}

}

- MrWayne August 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SumIsten 
{
    
    private int [] series;
    public SumIsten ()
    {
        series = new int[11];
    }
    
    public void IsTen(int n)
    {
        if(series[10-n]!=0)
        {
            System.out.println("true");
            return;
        }
        else{
            series[n]= series[n]+1;
        }
    }
    
    public  void printSoFar()
    {for(int i=0;i<series.length;i++)
        {
            for(int j=0;j<series[i];j++)
            System.out.println(i);
        }
    }
}

- karldren8 August 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

another approach

there are 11 possibilities, just use a 16 bit integer where each bit is representing a value.
If (aNumber>>11) & 1 return true;

- JustAnIdea September 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just deal with numbers <=10 since only +ve numbers are incoming.

int array[]={6,4,2,9,1,8,7,3,5};
		HashMap<Integer,Boolean> map=new HashMap<Integer,Boolean>();
				
		for(int i=0;i<array.length;i++)
		{
                        if(array[i]>10)continue;			
			if(map.containsKey(10-array[i]))
			{
				System.out.println("found " + array[i] + " and "+ (10-array[i]));
			}
			map.put(array[i], true);
		}

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

__author__ = 'shubham.saxena'

dicti = {}
while(1):
t = int(raw_input())
key = 10-t
if(key in dicti.keys()):
print True
break
else:
print False
dicti[t] = 1
print dicti.keys()

- shubh2506 December 07, 2016 | 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