Microsoft Interview Question for Software Engineer / Developers






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

Solution:

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

namespace MSIntervewPreparation
{
    class CaesarCipherAlgorithm
    {
        private static int shift = 3;

        static void Main(string[] args)
        {
            //Caesar cipher
            string stringToEncrypt = "the quick brown fox jumps over the lazy dog";
            Console.WriteLine("\nThe String To Encrypt: " + stringToEncrypt);

            string encryptedString = Encrypt(stringToEncrypt);

            Console.WriteLine("\nThe resultant Encrypted string is: " + encryptedString);

            Console.WriteLine("\nThe String To Decrypt: " + encryptedString);

            string decryptedString = Decrypt(encryptedString);

            Console.WriteLine("\nThe resultant Decrypted string is: " + decryptedString);
            Console.WriteLine();
        }

        private static string Decrypt(string stringToDecrypt)
        {
            StringBuilder decryptedString = new StringBuilder();
            foreach (char ch in stringToDecrypt)
            {
                int asciiCH;
                char decryptedChar;
                if (char.IsLower(ch))
                {
                    asciiCH = ((int)ch - (int)'a' - shift) % 26;
                    if (asciiCH < 0) asciiCH += 26;
                    decryptedChar = (char)(asciiCH + (int)'a');
                }
                else
                {
                    if (char.IsUpper(ch))
                    {
                        asciiCH = ((int)ch - (int)'A' - shift) % 26;
                        if (asciiCH < 0) asciiCH += 26;
                        decryptedChar = (char)(asciiCH + (int)'A');
                    }
                    else
                    {
                        decryptedChar = ch;
                    }
                }
                decryptedString.Append(decryptedChar.ToString());
            }
            return decryptedString.ToString();
        }

        private static string Encrypt(string stringToEncrypt)
        {
            StringBuilder encryptedString = new StringBuilder();
            foreach (char ch in stringToEncrypt)
            {
                int asciiCH;
                char encryptedChar;
                if (char.IsLower(ch))
                {
                    asciiCH = ((int)ch - (int)'a' + shift) % 26;
                    encryptedChar = (char)(asciiCH + (int)'a');
                }
                else
                {
                    if (char.IsUpper(ch))
                    {
                        asciiCH = ((int)ch - (int)'A' + shift) % 26;
                        encryptedChar = (char)(asciiCH + (int)'A');
                    }
                    else
                    {
                        encryptedChar = ch;
                    }
                }
                encryptedString.Append(encryptedChar.ToString());
            }
            return encryptedString.ToString();
        }
    }
}

- rahultelang November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void encrypt_decrypt_String(int kShift, int pOption);

int main()
{
	int option = 0;
	int shift = 0;
	printf("Enter the shift for caeser cipher..\n");
	scanf("%d",&shift);
	printf("Enter 1 to encrypt and 0 to decrypt..\n");
	scanf("%d",&option);
	while(getchar() != '\n');
	encrypt_decrypt_String(shift, option);
}

void encrypt_decrypt_String(int kShift, int pOption)
{
	char ch;
	ch = getchar();
	while(ch != '\n')
	{
		if(ch == ' ')
		{
			putchar(' ');
		}
		else
		{
			if(pOption == 1)
				putchar(ch + kShift);
			else if(pOption == 0)
				putchar(ch - kShift);
			else
			{
				printf("Option not supported..\n");
				break;
			}
		}
		ch = getchar();
	}
	putchar(ch);
}

- Jit November 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code with a little modification..

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

void encrypt_decrypt_String(int kShift, int pOption);

int main()
{
	int option = 0;
	int shift = 0;
	printf("Enter the shift for caeser cipher..\n");
	scanf("%d",&shift);
	printf("Enter 1 to encrypt and 0 to decrypt..\n");
	scanf("%d",&option);
	while(getchar() != '\n');
	encrypt_decrypt_String(shift, option);
}

void encrypt_decrypt_String(int kShift, int pOption)
{
	char ch;
	ch = getchar();
	while(ch != '\n')
	{
		if(ch == ' ')
		{
			putchar(' ');
		}
		else
		{
			if(pOption == 1)
			{
				if(isupper(ch))
				{
					putchar('A' + (ch + kShift - 'A')%26);
				}
				else
				{
					putchar('a' + (ch + kShift - 'a')%26);
				}
			}
			else if(pOption == 0)
			{
				if(isupper(ch))
				{
					putchar('Z' + (ch - kShift - 'Z')%26);
				}
				else
				{
					putchar('z' + (ch - kShift - 'z')%26);
				}
			}
			else
			{
				printf("Option not supported..\n");
				break;
			}
		}
		ch = getchar();
	}
	putchar(ch);
}

- Jit November 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void ceaser ( char* string )
{
While ( *string ! = NULL )
{ int offset = *string – ‘A’ ;
if( offset < 24 ) *string = *string + 3
else /* *string is X,Y,Z */
{
*string = ‘A’ + ( (offset + 3) % 26 ) – 1
}
}

- yeda_anna November 12, 2010 | 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