Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Build a hash look up table of all chars in src string. Then scan find string and return index of first char that exists in look up table.

- tom February 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes..simple as that. However, I guess KMP is a standard answer for this question.

- Anonymous February 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Would the complexity be O(n+m) while implementing using a hashtable or O(1)? HashTable operations are O(1) but are we not going to write a loop to add and check the values so won't the complexity be O(m+n)?

- Anonymous February 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the most difficult part of this question is its English :). What it asks is to return the index of the 1st character in "src" that matches any character in "find".
KMP can work with a variant, such that if ith element in "src" does not match anything in "find", starts from src[i+1] and go through KMP again. However, you will need to rebuild the partial match table, and the run time is at least O(n^2).
I think if I am asked this question again, I'd ask if the strings are ASCII. If they are, I can simply build a hashtable with "find" with 256 buckets using the characters as the key. Then loop through "src" while doing search in the hashtable. This will be O(n+m) with n=src.length and m=find.length. Similarly, instead of a hashtable, I can use a 256 element interger/bit array.
Your thoughts?

- IHE February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes I agree, using KMP it will be O(n^2), because we will need to check for each character in the find. KMP is O(n) and in worst case if we need to search till nth character of find it will be O(n^2)

- jerry February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Please read the the problem well :) Kmp is too bid cannon for this!

The fastest is: as there are less than 32 character types (let's suppose it is not Unicode) you can represent the search values in an integer: go through the find array and OR it's shifted values to the integer, marking the bit 1 if it's a searchable char (a> 0001 b>0010 ...)

Then start processing the the input, AND ing all shifted values to this array. If the result is greater than 0 that is your first match.

Please note that even though hashtable is considered to be O(1) no interviewer will accept the answer for a fixed size input as optimal ;)

- Adam February 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maybe, we can also sort the src string, and for each character in finding string, we perform a binary search in src string to see if src string also has such character. It will be slower, however, save spaces

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

- Add every char from src to unordered_hashmap with the char as key and no of times it occurs in the src string as value
- hence, src will be transformed as (g,2) (a,2) and (b,2) in the map.
- now check if every char from "find" string is is present in the map.
- if any char is found, find the index of that char in the "find" string and thats it !
- fetching from map is O(1)
- looking up every char from "find" takes O(n) where n=strlen(find)
- additional space of O(n) for the map !

- Rahul Arakeri August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use KMP to do it in o(n).

- Pankaj February 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And how exactly do you use KMP to solve this problem? Can you cook up some code. What if this is the case:

SRC: acdgef
FIND: opg

- P.M February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

CAA: Create Auxilary Array
MFC: Match First Character
a: find string
b: src sring
c: aux string
t: temp int va;ue
CAA()
	c[n] ={0}
	for each i from 0 to n-1
		t = a[i] - 'a'
		c[t] ++

MFC()
	for each i from 0 to m-1
		t = b[i] - 'a'
		if(c[t] != 0)
			print b[i]
			return

main
	CAA()
	MFC()

- Prateek Caire February 17, 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