Amazon Interview Question


Country: India




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

Take each word, sort it and put it in a hashmap with key => sorted word & value => original word. Thus the hashmap will look like

aann --> anna, nana
dhjss -> hjsds
eerww -> werwe, eerww
adss -> sads

Now iterate through the hashmap and print all the values. O(n) solution

- rookie.coder March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution will not run in linear time. You forgot to account for the time to sort each word.

- James March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorting a word is linear in the number of chars.

- david.rebatto@gmail.com March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, I agree. It is O(n lg n). Does anyone have an O(n) solution?

- rookie.coder March 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not O(n lg n). It is more like O (n* m lg m) where m is the max number of characters possible in the words. Now, if m is small, m lg m will be just like a small constant and thus it will be O(n)

- yossee March 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@yossee: That's the correct analysis.


O(n) isn't going to be achievable because there's O(m*n) bits of data to look at. O(m*n) complexity is the lowest you can hope for, so O(n*mlogm) is really pretty close to optimal.

- eugene.yarovoi April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) solution is available if we use hash table... n is number of strings in the string array.....

- Hari Haran(USA) April 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort them based on signature. And then print
So Anna --> aann
Nana --> aann
And so on

- Anil March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For each word, make a integer array 26 long. Scan through each word and for each letter x, lowercase the letter and increment the value at index[Ascii(x) - Ascii('a')] by 1. Now use the string representation of the array as the key to look up entries in a hash table. If an entry exists, print out this word and the value in the hash table. If not, load the hash table with the string version of the array as the key and the word as the value.

- chico March 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For each word, make a integer array 26 long. Scan through each word and for each letter x, lowercase the letter and increment the value at index[Ascii(x) - Ascii('a')] by 1. Now use the string representation of the array as the key to look up entries in a hash table. If an entry exists, print out this word and the value in the hash table. If not, load the hash table with the string version of the array as the key and the word as the value.

- Chico2000 March 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Scan through the list of words find the sum of each word by adding the ascii values and add it simultaneously to hashmap with key as sum of word nd value as word if collision occurs print the current word nd the stored one. Order is O(n) .assumption n(no of words) > m(length of any word).

- Anonymous March 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This approach would not work, and I'll provide an example

The String "22" has sum of ascii value 100 ('2' has ascii value of 50). This would collide with the String "d", which has ascii value of 100 by itself. However, I agree with the idea of producing an order-irrelevant hash value for each string.

Instead of using sum as keys, you can design a data-structure that removes the ordering and use the data-structure as key.

e.g. hashMap<char, int>, you would increase the int each time the same char appears. Depending on the "resource vs time" argument, you can also use an array or int instead (ascii table has 127 char in total, so you would use array with 127 elements).

- Emkay April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I completely agree with the above comment.
The idea of using some hash function to use as the key for values is great. Obviously, the sum of ascii values does not prove to be a good key value. I got some idea from the RK algorithm.

We can use double-hash or triple-hash if required to get a unique key value. Adding the ascii values can be one technique. If it matches, then we can apply another hash technique on top of that. The other hash technique has to be thought of and can be anything like:
"Anna" -> ascii(A) - ascii(n) + ascii(n) - Ascii(A).
This is just an example and maintain another hashmap for these entries.
If our string to be checked matches all these criterions, then we assume it is an anagram and print the value for that key and our word under consideration.

This provides a full proof method for avoiding wrong analysis of anagrams, though the storage is a little bit higher but good time complexity.

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

static void printAnagrams(String[] words) {
HashMap<Integer, String> hash = new HashMap<Integer, String>();

for(String s : words) {
int code = getCode(s);
if(hash.containsKey(code)) {
System.out.println(s + " and " +hash.get(code) + " ");
hash.remove(code);
} else {
hash.put(code, s);
}
}
}

static int getCode(String s) {
int code = 1;
for(char c : s.toCharArray()) {
code *= prime[c-'a']; // array of primes
}
return code;
}

- Anonymouse April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.lang.String;
import java.util.*;
public class similiar
{
public static Hashtable store = new Hashtable();
public static void pairs (String arg)
{
double temp = convert(arg);
if(store.containsKey(temp))
System.out.println(store.get(temp)+" , "+arg);
else
store.put(temp,arg);
}
public static double convert(String args)
{
double output=0;
int lengthofargs = args.length();
int j=0;
for( j=0;j<lengthofargs;j++);
{
j-=1;
int temp=(int)args.charAt(j);
temp-=96;
output+=Math.pow(10,temp-12);
}
return output;
}

public static void main(String args[])
{
for(String arg:args)
{
pairs(arg);
}
}
}

- O(n) code April 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) code

import java.lang.String;
import java.util.*;
public class similiar
{
public static Hashtable store = new Hashtable();
public static void pairs (String arg)
{
double temp = convert(arg);
if(store.containsKey(temp))
System.out.println(store.get(temp)+" , "+arg);
else
{
store.put(temp,arg);
// System.out.println("temp : " +temp + " arg : " + arg);
}
}
public static double convert(String args)
{
double output=0;
int lengthofargs = args.length();
int j=0;
for( j=0;j<lengthofargs;j++)
{

int temp=(int)args.charAt(j);
temp-=96;
output+=Math.pow(10,temp-11);
// System.out.println(j);
// System.out.println("char :" +args.charAt(j)+" output : " + output);
}
return output;
}

public static void main(String args[])
{
for(String arg:args)
{
pairs(arg);
}
}
}

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

garimaa.blogspot.in/2012/04/program-10th-in-c_10.html

- Solution April 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create hashcode method according to prime numbers:
for example :
a->3
b->7
so hashcode for
ab ->21
ba -> 21

so they will collide then u will print them as an anagram

- Elbek June 03, 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