Microsoft Interview Question for Software Engineer / Developers






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

i think this question frequently asked in ms
void swap(char *s,int i,intj);
{
char temp;
temp=s[i];
s[i]=s[j];
s[j]=temp;

}

void Permutaions(char str[],int i)
{
int n;
int k;
n=strlen(str);
if(i==n)
{
puts(str);
return;
}
for(k=i;k<=n-1;k++)
{
swap(str,i,k);
Permutaion(str,i+1);
swap(str,i,k);
}
}

- geeks July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That will print out the same permutations if chars are repeated.

- memo July 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PermuteString {

	public static void main(String[] args) {
		permute("4aaabbb");
	}
	
	public static void permute(String s) {
		if(s == null) return;
		StringBuilder sb = new StringBuilder(s);
		System.out.println(sb.toString());
		while(true) {
			int i;
			for(i = sb.length()-1; i > 0; i--) {
				if(sb.charAt(i-1) < sb.charAt(i)) break;
			}
			i--;
			if(i == -1 ) break;
			int j;
			for(j = sb.length()-1; j > i; j--) {
				if(sb.charAt(i) < sb.charAt(j)) break;
			}
			swap(sb, i, j);
			reverse(sb, i+1, sb.length() - 1);
			
			System.out.println(sb.toString());
		}
	}
	private static void swap(StringBuilder sb, int i, int j) {
		char c;
		
		c = sb.charAt(i);
		sb.setCharAt(i, sb.charAt(j));
		sb.setCharAt(j, c);
	}
	private static void reverse(StringBuilder sb, int i, int j) {
		for(int k = 0; k < (j-i+1)/2; k++) {
			swap(sb,i+k,j-k);
		}
	}
}

- Anonymous July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think in your algo if you pass "zpa" it will print nothing.

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

If you want to deal with same character, you can use next_permutation in STL.
Below is the implementation of next_permutation

template <typename BidirectionalIterator>
bool next_permutation(BidirectionalIterator first, BidirectionalIterator last)
{
   if(first == last)
       return false;
   BidirectionalIterator i = first;
   ++i;
   //if only one element
   if(i == last) 
     return false;
   i = last;
   --i;
   for(;;)
   {
      BidirectionalIterator j = i;
      --i;
      if(*i < *j) 
      {
         BidirectionalIterator k = last;
         while(!(*i < *--k))
            ;
         std::iter_swap(i,k);
         std::reverse(j,last);
         return true;
      }
      if(i == first)
      {
           std::reverse(first, last);
           return false;
       }
    }
}

- nim September 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you please explain what exactly are you doing inn here. unable to fathom the code

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

#include <stdio.h>
#include <string.h>

static void permuteStringImp(char *s, int pos, int len);
static void swap(char *s, int i, int j);

void permuteString(char *s, int len)
{
        if(s == NULL || s[0] == '\0' || len <= 0)
                return;

        permuteStringImp(s, 0, len);
}

static void permuteStringImp(char *s, int pos, int len)
{
        int i;
        if(pos >= len)
        {
                printf("%s\n", s);
                return;
        }

        for(i = pos; i < len; i++)
        {
                swap(s, pos, i);
                permuteStringImp(s, pos+1, len);
                swap(s, pos, i);
        }
}

static void swap(char *s, int i, int j)
{
        char temp;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
}

- Anonymous September 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void permute(string rem, string prefix, vector<string>& permutations)
{
	string::iterator it = rem.begin();
	if(it == rem.end())
		permutations.push_back(prefix);
	while(it != rem.end())
	{
		string newrem(rem.begin(), it);
		newrem += string(it + 1, rem.end());
		permute(newrem, prefix + *it, permutations);
		it++;
	}
}

- Anonymous March 18, 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