Microsoft Interview Question






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

o(1) space with o(n) time. Just start at the end of the sentance and scan to the beginning searching for the character. Optimal in regards to space and time.

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

how is that o(n) in time. You need to first go all the way to the end, which is O(n) itself. And if the last occurence of the character is the first letter itself (for example, last occurence of 'C' in "Cars are fast and expensive") , u hav to go all the way back to the beginning => O(n^2).

- Jk April 27, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In your solution, we need n+n operation, it is still O(n)

- liandage May 20, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope O(1) is correct. If chars are stored in an array, you can jump to last element in O(1) time.

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

how can you jump to the last in O(1) time? you need to know the length of the string right? it takes O(n) time to find out the length of the string. As mentioned by JK, what if the char is at the beginning only? your solution is O(2n) but if you move from the start to end it is O(n) which is better than O(2n).

- Ramu September 29, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Jk, learn how to analyze runtimes. Apparantly, your knowledge about it sucks.

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

int lastOccurrence(char *str, char ch){

if(str==NULL)
return -1;

int pos=strlen(str)-1;
while( pos>=0 ){
if(str[pos]==ch)
break;
pos--;
}
return pos ;
}

- Raman November 24, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this algo's running time would be O(n) and space complexity would be O(1)

- Raman November 24, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int pos=strlen(str)-1; //WRONG
 int pos=strlen(str);   //RIGHT

// strlen excludes '\0' nul character//

- Carbon April 03, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

strlen takes O(n) time...so i guess its O(2n) solution.

- Rohit July 08, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple O(n) solution :

int get_position(char str[], char ch)
{
    int i, pos = 0;
    
    for(i = 0; str[i] != '\0' && i < MAX_LENGTH-1; i++)
    {
        if(str[i] == ch)
            pos = i+1;
    }
    
    return pos;
}

- Srikant Aggarwal December 21, 2011 | 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