Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

I didn't know what a blossom filter was. I just learnt a few things and I want to give it a try. Would this make sense?

Given that we are working with ASCII from 97 to 122, we can consider that the changing bits for each letters are the 5 least-significant (the ones to the right in a little-endian machine).

So we can use an array of 20 bits and just assign directly.

int table = 0;

// assume we check the word is 4 chars somewhere else
int hash(char * str) {
	int h = 0;
    h |= ((int)(char[0] & 0b11111)) << 15;
	h |= ((int)(char[1] & 0b11111)) << 10;
	h |= ((int)(char[2] & 0b11111)) << 5;
	h |= ((int)(char[3] & 0b11111)) << 0;
	return h;
}

// assume str is 4 char
void put(char * str) {
	table |= hash(str);
    putInSet(str);
}

int maybeInSet(char * str) {
	// if not 4 char return 0

	// else
	return (table & hash(str)) == table;
}

}

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

Sorry, the returned value for maybeInSet should be computed:

return (table | hash(str)) == table;

- Anonymous March 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

All 4 chars fit in an integer, so:

int hash(char * str) {
        int h = ((int)char[0]) << 24;
	h |= ((int)char[1]) << 16;
	h |= ((int)char[2]) << 8;
	h |= ((int)char[3]) << 0;
	return h;
}

- Anonymous March 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So the trick of the Bloom filter is that you only want each word to set a few bits in the filter. If each word leads to an average of ten bits being set in the filter, it doesn't take too many words until every bit in the shared bitmap is 1, at which time you effectively get positive guesses every time, which means way too many false positives.

- showell30@yahoo.com March 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Makes sense. What about something like:

int hash(char * str) {
        int h = 0;
	h |= bit(char[0]);
	h |= bit(char[1]);
	h |= bit(char[2]);
	h |= bit(char[3]);
	return h;
}

int bit(char c) {
	srand(c);
	int shift = randInRange(0, 31);
	int b = 1;
	return b << shift;
}

- Anonymous March 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Here's a Java implementation
	static int MAX_BYTE = 488;
	static int MIN_BYTE = 388;             // i.e., "aaaa"
	static int[] keys = new int[100];        // used because MAX_BYTE - MIN_BYTE
	static String[] words = {"able", "band", "bend", "card", "darn", "form", "grab", 
				"harm", "look", "make", "mate", "peer", "team"};

	public static void main(String[] args)
	{
		createBloom(words);
		

		System.out.println("may contain: " + "abbe" + mayContain("abbe"));
	}
	

	public static void createBloom(String[] values)
	{
		for(int i = 0; i < values.length; i++)
		{
			int key = getKey(values[i].getBytes());
			if(keys[key - MIN_BYTE] == 0)
			{
				keys[key - MIN_BYTE]++;
			}
		}
	}

	public static int getKey(byte[] bit)
	{
		int out = 0;
		for(int i = 0; i < bit.length; i++)
		{
			out += bit[i];
		}
		return out;
	}

	public static boolean mayContain(String query)
	{
		int keyCheck = getKey(query.getBytes());		// find query key
		return keys[keyCheck - MIN_BYTE] == 1;
	}

- Chris Sullivan March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int hash[4][26];

void put_in_set(char *word)
{
    int i = 0;
    while(word && i<4)
    {
            hash[i][*word - 'a'] = 1;
            i++;   
    }
}

int am_in_set(char *word)
{
    int ret = 0;
    int i    = 0;
   
    while(word && i < 3)
    {
         if( hash[i][ *word - 'a'] == 0 )
              break;
    }
    if( i == 3 )
       ret = 1

    return ret;
}

- Anonymous July 04, 2013 | 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