Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

C# ( for MS :D )

public bool isPalindrome(inputStr){
if (inputStr.length == 0)
{
    console.writeLine("invalid input");
    return false;
}
    for (int i = 0; i<=inputStr.length/2; i++){
        if (inputStr[i] != inputStr[inputStr.length - 1 - i])
            {return false;}
    }
    return true;
}

- john madden john madden john madden October 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just FYI, most interviewers will expect you to do a static test of your code. At very least you will want to pass even and odd length palindromes and non-palindromes, plus various corner cases like empty string and single-character. Doing this, you would spot that this code does a meaningless final comparison for all odd-length palindromes (including single character input)--how can a character not be equal to itself? You should also provide a type for inputStr; You never know if your interviewer might try to type it in verbatim and compile it, or what they consider excusable.

- Adam Smith October 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public boolean isPalen(String str){
  if(str == null){
    throw new NullPointerException();
  }
  int start = 0;
  int end = str.length() -1;
  char[] chars = str.getChars();
  while(start < end){
    if(chars[start++] != chars[end--]){
      return false;
    }
  }
  return true;
}

- Anonymous October 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	string str;
	cin>>str;
	
	int i=0,j=str.length()-1;
	
	int k=1;
	while(i<=j)
	{
		if(str[i]==str[j])
		{
			i++;
			j--;
		}
		else
		{
			k=0;
			break;
		}
	}
	if(k==0)
	  cout<<"Given string is not a palindrome";
	else
	  cout<<"Given string is palindrome";
	  
	return 0;
	
}

- anonymous October 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

While your main() function is technically a function which detects a palindrome, you will be expected to understand that you're being asked for a utility function that takes some manner of string as an argument and returns a boolean or equivalent enum value.

Secondly, the <= in the while loop condition results in one extra iteration of the loop for odd-length strings, wherein you check the middle character against itself unnecessarily.

Lastly, once converted to a utility function there will be no need for the allocation of k, or the if-then after the loop, since you'll exit with a return false where you have the k=0, or return true after the loop. Also, allocating an int where only a bool is needed is poor form, since an int can be much larger than a bool on some systems (memory wasted). Using appropriate types makes for better portability and readability.

- Adam Smith October 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class checkIfPalindrome
{
    public static void main(String[] args)
    {
    System.out.println(check("teet"));
    }

   static boolean check(String str)
    {
       if(str.length()==0) return false;
      for(int i=0;i<str.length()/2;i++)
        {
           if(str.charAt(i)!=str.charAt(str.length()-1-i))
               return false;
        }
            return  true;
    }
}

- Anonymous November 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <string>
using namespace std;


bool isPalindrome(string str)
{
	int len=str.length();
		for(int i=0;i<len;i++){
			if(str[i]!=str[len-1-i])
				return false;
		}
	return true;
}	

int main(int argc, char* argv[])
{

    cout << isPalindrome(argv[1]) << endl;
    return 0;
}

- Calypso January 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isPalindrome(String input) {
	String value = input;

	if(value.reverse().equals(input)) 
		return true;
	return false;
}

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

here is the quick algorithm

N = InputStr.Length;
If N%2 = 0 Then i = N/2-1; j = N/2;
else i = N/2-1;j = N/2+1;
while(i>=0)
	If A[i] == A[j]
		i--;j++;
	else
		Print "Not"
		return;
	end
end
Print "Yes"

- sonesh June 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string mainString = "sasas";
            char[] mainArr = mainString.ToCharArray();
            StringBuilder sb = new StringBuilder();

            for (int i = mainArr.Length - 1; i >= 0;  i--)
            {
                sb.Append(mainArr[i]);
            }

            if (mainString == sb.ToString())

                Console.WriteLine("Palindrome");
            else
                Console.WriteLine(" no Palindrome");
            Console.Read();

- Anonymous July 04, 2015 | 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