Amazon Interview Question for Software Engineer in Tests






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

hash every word in the string ??

- knap August 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

push all the words in a HashTable and return the HashTable.length().

- addy August 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If your dictionary is represented as a trie it is easy. For each position in the string, begin the word there descending down the trie as you go and recording which nodes you reach that are words.

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

generate all combinations of the string and check against a dictionary if a given combination is a valid word and keep a count. Off course the dictionary should be provided and it can be in a hash table format so that the lookup is constant time.

- hugh August 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice!

- Anonymous January 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i dont understand y using a predefined dictionary....they are not asking for valid words...they are just asking for unique words...i think the ques is similiar to sayin find uniqe elements in an array only that the elements of an array are strings now..instead of chars....what say??

- utscool February 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// company_project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <vector>
#include <set>
#include <string>
#include <list>
#include <iostream>
#include <sstream>
#include <map>


// algorithm 1
unsigned int numUniqueElements(std::string &s) {
std::map< std::string, bool > stringPresenceMap;
std::istringstream originalStringStream(s);
std::string word;
unsigned int count = 0;
while (originalStringStream >> word) {
if (stringPresenceMap.find(word) == stringPresenceMap.end()) {
stringPresenceMap[word]= true;
count++;
}
}
return count;
}
// test 1
void test1() {
std::string str1("This is goog goog good good");
std::cout << str1 << std::endl << numUniqueElements(str1);
str1.assign("donkey donkey donkey");
std::cout << str1 << std::endl << numUniqueElements(str1);
str1.assign("");
std::cout << str1 << std::endl << numUniqueElements(str1);
str1.assign("yahoo google yahoo google microsoft microsoft");
std::cout << str1 << std::endl << numUniqueElements(str1);
}

int _tmain(int argc, _TCHAR* argv[])
{
test1();
return 0;
}

- sachin sinh gaur August 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

None of the interviewers expect such complex code

- Anonymous September 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

answers from Anonymous and hugh are just BS.
the first 2 answers sounds simple and pragmatic..

- googler August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

agreed

- Anonymous October 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a hashset and add all words... find its size... you don't need hashmap for this.

- PrettyDumb September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question is asking for the number of unique words, but your answer seems that you already know these words "use a Hashset and add all words". How did you identify these unique words from the given string?

- nancie December 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

HashSet cannot have duplicate elements. Therefore when you add all the words in the string(could be duplicate), only unique words will be inserted. Hence size of the HashSet will be the answer.

int uniqueWords(String input)
{
Set<String> hs = new HashSet<String>();
String[] strings = input.split("");
for(String x: strings){
hs.add(x);
}
return hs.size();
}

- HashSet is the answer January 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

String in = "this is not madness this is sparta";

StringTokenizer st = new StringTokenizer(in);
Hashtable<String, Integer> h = new Hashtable<String, Integer>();

int count = 1;
while(st.hasMoreTokens()){
String s = st.nextToken();
if(!h.contains(s)){

h.put(s, count);
}

}
System.out.println(h.size());

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

Okay has table solution sounds perfect but can anyone answer what would be the size of hash table ? What is the hash function to hash strings ?

- M August 11, 2010 | 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