Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

Idea is very Simple,We can use the concept of subset generation but here for
0-Lower case
1-Upper Case
With the given example "the/THE"
000-the
001-thE
010-tHe
011-tHE
100-The
101-ThE
110-THe
111-THE

Hope the solution is clear

-Rex

- Rex September 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Using Recursion should yield the required answer

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

void toggler(char* x, int pos){
        if(pos==0){ printf("%s\n",x); return; }

//      printf("String is now: %s\n",x);
        x[pos-1] = toupper(x[pos-1]);
        toggler(x, pos-1);
        x[pos-1] = tolower(x[pos-1]);
        toggler(x, pos-1);

        return;
}

int main(void){
        char str[500];
        scanf("%s",str);
        toggler(str, strlen(str));
        return 0;
}

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

good job........!!

- MOZHI September 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanku...

- laasya1991 September 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Java Implementation based on this approach. Will also work if the string has numbers

public class CaseCombination {
	
	char[] str;
	
	public CaseCombination(char[] str){
		this.str = str;
	}
	
	public static void main(String[] args){
		
		System.out.println("Enter the string");
		Scanner input = new Scanner(System.in);
		
		String str = input.nextLine();
		
		input.close();
		CaseCombination thisObj = new CaseCombination(str.toCharArray());
		
		printCases(thisObj, thisObj.str.length);
	}

	private static void printCases(CaseCombination obj, int index){
		if(index == 0){
			System.out.println(obj.str);
			return;
		}
		if(Character.isAlphabetic(obj.str[index - 1])){
			obj.str[index - 1] = Character.toUpperCase(obj.str[index - 1]); 
			printCases(obj, index-1);
			obj.str[index - 1] = Character.toLowerCase(obj.str[index - 1]); 
			printCases(obj, index-1);
		} else {
			printCases(obj, index-1); // if this char is digit just recurse without generating upper and lower case 
		}
		return;
	}
}

- CodeNinja April 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintCases {
public static void main(String[] args) {
String word = "THE";
print(word, "", word.length());
}

public static void print(String str, String result, int length) {
        if (result.length() == length) {
            System.out.println(result);
        } else {
            int switchCase = (str.charAt(0) > 'a' ? 32 : -32);
            print(str.substring(1), result + str.charAt(0), length);
            print(str.substring(1), result + (char) ((int) str.charAt(0) - switchCase), length);
        }
    }
}

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

thanku..

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

#include <iostream>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>

using namespace std;

#define LOWER 0
#define UPPER 1
#define SIZE 100

void generate(char *s)
{
        int strLength = strlen(s);
        unsigned long maxValue = pow((double)2, strLength) - 1;

        for(int generator = 0; generator <= maxValue; generator++)
        {
                int i, j;
                bitset<SIZE> map(generator);
                i = strLength - 1;

                for(j = 0; s[j] != '\0' ; i--, j++)
                {
                        char c;
                        if(map[i] == LOWER)
                                c = tolower(s[j]);
                        else
                                c = toupper(s[j]);
                        cout << c;
                }
                cout << endl;
        }
}

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

#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;


void generateRec(char *s, int pos, int len)
{
        if(pos >= len)
        {
                cout << s << endl;
                return;
        }

        s[pos] = tolower(s[pos]);
        generateRec(s, pos+1, len);
        s[pos] = toupper(s[pos]);
        generateRec(s, pos+1, len);
}


int main(void)
{
        char str[] = "THE";
        generateRec(str, 0, strlen(str));
        return 0;
}

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

Using The truth table ideal. For example if we have the word "the" it corresponds to 3 bits. So the possible combinations are ranging from 000 -- 111. Where 000 is case0 and 111 is case7. Note that in number in the computer is represented inside the computer as binary. So, if we have num = 3. i.e., num = 0011. I will use some shifting and masking operations to do the task.


C# code
-------------

using System;
using System.Collections.Generic;
using System.Text;

namespace StringSmallCap
{
class Program
{
static void PrintCase(string str, int num)
{
int index = str.Length - 1;
char[] charArr = str.ToCharArray();

while(num != 0)
{
if ((num & 0x00000001) != 0)
charArr[index] = char.ToUpper(charArr[index]);
num >>= 1;

index--;
}
Console.WriteLine(charArr);
}
static void Main(string[] args)
{
Console.Write("Word: ");
string word = (Console.ReadLine()).ToLower();

Console.WriteLine(word);
for (int caseNum = 1; caseNum < Math.Pow(2,word.Length); caseNum++)
{
PrintCase(word, caseNum);
}
}
}
}

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

using System;
using System.Collections.Generic;
using System.Text;

namespace StringSmallCap
{
    class Program
    {
        static void PrintCase(string str, int num)
        {            
            int index = str.Length - 1;
            char[] charArr = str.ToCharArray();

            do
            {
                if ((num & 0x00000001) != 0)
                    charArr[index] = char.ToUpper(charArr[index]);

                index--;
            }
            while ((num >>= 1) != 0);

            Console.WriteLine(charArr);
        }
       

       

        static void Main(string[] args)
        {           
            Console.Write("Word: ");
            string word = (Console.ReadLine()).ToLower();

            Console.WriteLine(word);            
            for (int caseNum = 1; caseNum < Math.Pow(2,word.Length); caseNum++)
            {
                PrintCase(word, caseNum); 
            }

            
        }
    }
}

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

Hi,

Is there a way we can avoid recomputation of same substring again? Say the original string is 'the'.
Now we take two versions of t i.e. 't' and 'T' and append to it the result obtained with string being 'he'. Here we are computing for the same substring again.
Can memoization (as in Dynamic programming) be of some help?

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

TRY THIS ONE :)
JUST THE MODIFICATION REQUIRED IN TH ESUBSET PROBLEM :)
#include<string.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
void possible(char str[],int n,int i,char net[]);
char st[100];
char net[100];
int n;
gets(st);
n=strlen(st);
possible(st,n,0,net);



return 0;
}
void possible(char *str,int n,int i,char *net)
{
if(i==n)
{
puts(net);
return;

}
net[i]=str[i];
possible(str,n,i+1,net);
net[i]=str[i]+'a'-'A';
possible(str,n,i+1,net);

}

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

#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace std;
//TASK print all combinations of upper/lowercase letters
int PrintAll(const char *s, int size)
{
    if (!s) return 0; // nothing to print
    if (size < 1) return 0; // nothing to print
    if (size > 31) return 0; // too much to print, does not fit in x86 int
                             // also, why are we printing 4 billion strings?
                             // perhaps management should think this through
                             // before deciding to print
    int max = pow(2.0, size);
    // or int max = 1<<size;
    for (int i=0; i<max; i++)
    {
        int tmp = i;
        for (int j=0; j<size; j++)
        {
            if (tmp&0x1)//potentially depends on 1-byte addressable memory
            {
                putchar(toupper(s[j]));
            }
            else
            {
                putchar(tolower(s[j]));
            }
            tmp = tmp>>1;//divide by two -- bit-shift 1 bit
        }
        printf("\n");
    }
}

int main(int argc, char *argv[])
{
    const char* b = "FOOBAR";
    PrintAll(b, strlen(b));
    system("PAUSE");
    return EXIT_SUCCESS;
}

- C++ Lion September 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char s[100];
cin >> s;

for (i = 0; i < pow(2, strlen(s)); i++)
{
val = i;
for (j = 0; j < strlen(s); j++)
{
if(val & 1)
print lower(s[j]);
else
print upper(s[j]);
val >>= 1;

}
}

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

#include <iostream>
#include <string>
#include <vector>
std::vector <std::vector<char>>myChars(26,2);

void makeCombination(const std::string &str, int index,const int &length, std::string combination){
	if(index == length){
		std::cout << combination <<std::endl;
		return ;
	}
	for(int i = 0; i < myChars[str[index] - 'a'].size(); ++i){
		makeCombination(str,index + 1, length,combination + myChars[str[index] - 'a'][i]);
	}
}
void makeCombinationsAux(const std::string & str){
	std::string temp = str;
	for(int i = 0; i < str.length(); ++i){
		if(str[i] <= 90){
			temp[i] = str[i] + 32;
		}
	}
	std::string combination = "";
	makeCombination(temp,0,temp.length(),combination);
}


int main(){
	for(int i = 0; i < myChars.size(); ++i ){
		myChars[i][0] = 'A' + i;
		myChars[i][1] = 'a' + i;
		
	}
	std::string str = "The";
	makeCombinationsAux(str);
	std::cin.get();
	return 0;

}

- Abdul Samad April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Main code is in the function makeCombination the rest is just preprocessing

- Abdul Samad April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = 'abcd'
b = list(a)
def comb(b, i):
    if i < 0:
        print(b)
        return
    b[i] = b[i].lower()
    comb(b, i-1)
    b[i] = b[i].upper()
    comb(b, i-1)
    return

- myarycn March 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
	// String to be printed in upper and lower case permutation and combination. 
	// Asuming input is always lower case. 
	char strarr[] = "the";

	int strlen = 0;
	char* tempstr = strarr;

	// Calculating the length of the string. 
	while (*tempstr != '\0')
	{
		strlen++;
		tempstr++;
	}

	// Calculating the number of permutation = strlen^2 - 1.
	int num_comb = strlen * strlen -1;

	// Calculating the difference between upper and lower case characters ASCII value.
	int diff = (int)'a' - (int)'A';

	for (int r = 0; r < num_comb; r++)
	{
		for (int l = 0; l < strlen; l++)
		{
			// Checking if the bit is set, if set then upper case.
			if (r & 1 << l)
			{
				printf("%c", ((int)strarr[l] - diff));
			}
			else
			{
				printf("%c", strarr[l]);
			}
		}
		printf("\n");
	}

    return 0;
}

- -cninja November 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Neat solution. Used the above python code

a = 'the'
b = list(a)
def comb(b, i, res):
    if i < 0:
        res.append("".join(b))
        return
    b[i] = b[i].lower()
    comb(b, i-1, res)
    b[i] = b[i].upper()
    comb(b, i-1, res)
    return
res = []
comb(b,len(a)-1, res)
print res

- revanthpobala February 05, 2017 | 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