Amazon Interview Question for Software Engineer / Developers






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

import java.util.*;
public class hash {

public static void main(String[] args) {


String str[] = {"russia","japan","russia","india"};
int len = 4;
Hashtable ht = new Hashtable();
int i=0;
while(i<len)
{

String c = str[i];
System.out.println("c :"+c);
Integer intg = (Integer) ht.get(c);

if(intg ==null)
ht.put(c,new Integer(1));
else
ht.put(c,new Integer(intg.intValue()+1));

i++;
}

Enumeration k = ht.keys();

while(k.hasMoreElements()){
String key = (String) k.nextElement();
System.out.println( key + " > "+ ht.get(key) );
}
}

}

- Vikas June 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Solution is incomplete. You are not sorting the same value members.

- King@Work June 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why we need to sort ..?...same value members are getting hashed together...

- Anonymous June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the output of your solution will vary based on the different combination of same input.
but as per the question, the output:
1. should show the <country>:<count> based on the decreasing order of count.
2. and if there is a tie then show the country which comes first in alphabetic order.

- @vikas June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if you had japan and jamaica ? Both will be hased togather

- Anonymous June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if you had japan and jamaica ? Both will be hased togather

- Anonymous June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

After inserting all of them in the hashMap we can make tree sets of the same value types.

- King@Work June 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct algorithm is TRIE tree

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

Most efficient doesn't mean anything. Most efficient in space, time, what?

- memo June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This solution takes too much memory but the average time complexity is O(n log n)

Have a hash table with words as the key and its frequency as the value. Then have a binary search on the frequency of words, each node in the binary search tree will have two values one is the frequency of words on which the binary search tree is built and the other is reference to another binary search tree which contains the words for this frequency.

When ever we encounter insert a word into the hash table insert it in the binary search tree also and whenever a word s frequency is altered in the hash table , lets say a word 'japan' frequency is increased from 2 to 3 , then delete japan from the 'word binary search tree' which referenced by the 'frequency binary search tree' node which has the value of 2 and insert into the 'word binary search tree' referenced by the 'frequency binary search tree' node which has got the value 3 in it.
To display the the output in the desired format do post-order traversal of the 'frequency binary search tree' and in-order traversal of the corresponding 'word binary search tree' pointed each node of the 'frequency binary search tree'

- scot June 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the time complexity should be O(mlogn) (upper bound) where m is length of largest string in the set. In O("n"logn) first n does not make any sense.Tell me if i am wrong.

- mohit89mlnc July 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<fstream>
#include<map>
#include<vector>
using namespace std;

int main()
{
	fstream my;
	my.open("input.txt", fstream::in);
	map<string, int> mymap;
	map<int, vector<string> > outmap;
	map<string, int>::iterator it;
	string s;
	while( !my.eof() )
	{
		s.clear();
		my >> s;
		if( s.empty() )
			continue;
		it = mymap.find(s);
		if( it == mymap.end())
			mymap[s] = 1;
		else
			it->second++;
			
	}
	vector<string> temp;
	map<int, vector<string> >::iterator mit;
	for( it = mymap.begin(); it != mymap.end(); it++){
		temp.clear();
		temp.push_back(it->first);
		
		mit = outmap.find( it->second );
		if( mit == outmap.end())
			outmap[it->second] = temp;
		else
			outmap[it->second].push_back(it->first);
	}
	map<int, vector<string> >::reverse_iterator rit;
	vector<string>::iterator vit;
	for( rit = outmap.rbegin(); rit != outmap.rend(); rit++ )
		for( vit = rit->second.begin(); vit != rit->second.end(); vit++)
			cout << "\n" << *vit << " : " << rit->first;


	cout << endl;
	return 0;
}

input.txt contains

austin
japan
usa
japan
russia
usa
japan
japan
australia

- Aditya June 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is the time ans space complexity of this code ?

- ashish February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class algo {

static class node implements Comparable<node>{
String data;
Integer count;
public node(String data,Integer count){
this.data = data;
this.count =count;

}

public int compareTo(node obj){
if(this.count==obj.count)
return this.data.compareTo(obj.data);//in ascending order of name
else
return obj.count.compareTo(this.count);// in decending order of count

}

public String toString(){
return this.data+":"+this.count;
}

}

public static void main(String[] args) {

String str[] = new String[] { "japan", "usa", "japan",
"russia", "usa", "japan", "japan", "australia" };

HashMap<String, Integer> hm = new HashMap<String, Integer>();

for(String s:str){
hm.put(s,hm.get(s)==null?1:hm.get(s)+1);
}

Iterator<String> itr = hm.keySet().iterator();
Set<node> s = new TreeSet<node>();

while(itr.hasNext()){
String key = itr.next();
s.add(new node(key,hm.get(key)));
}

System.out.println(s);
}
}

- varun.gtbit@gmail.com June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution dude!!!

- chandan prakash July 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice! exactly what I had in mind.

- Karthik August 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice dude

- jai October 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

create an array of frequencies as well as string names from the input text.
not sort the array based on the following comparator

bool operator < (const void *a, const void *b ) {
int x;
if (a->frequency < b->frequency)
return 1;
if (a->frequency > b->frequency)
return -1;
if (a->frequency == b->frequency) {
x = strcmp(a->name, b->name);
if (x<0) 
return 1;
if (x>0)
return -1;
if (x==0)
return 1;
}

- Amm June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hash table and max heap.
In hash table the key will be word and value will the address of the node in max heap(which has the count).if the word exist increase the count in heap and heapify .after reading the whole file delete root and heapfify( n times).heap should also has the index of word in hash table.to print the word at the end.

- leet September 29, 2012 | 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