Microsoft Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

O(N^2)

public static void main(String[] args) {
		String str = "aabcbaaaaaaaaaesdgttgd";
		String r = longestPalindromeSubstring(str);
		r = r.replace("#", "");
		System.out.println(r);
	}

	// aabcb
	public static String longestPalindromeSubstring(String str) {
		String s = "#";
		for (int i = 0; i <= str.length() - 1; i++)
			s += str.charAt(i) + "#";

		char[] carr = s.toCharArray();
		int n = carr.length - 1;

		String lsp = "";
		int i = 0;
		while (i <= n) {
			int p = i - 1;
			int q = 0;
			String palin = String.valueOf(carr[i]);
			while (p >= 0) {
				q = 2 * i - p;
				if (p >= 0 && q <= n && carr[p] == carr[q])
					palin = s.substring(p, q + 1);
				else
					break;
				p--;
			}
			i++;
			if (palin.length() > lsp.length())
				lsp = palin;
		}
		return lsp;
	}

- sudip.innovates October 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ /** * Compare the chars at the given position are equal */ const charsAreEqual = (str, leftRide, rightRide) => str.charAt(leftRide) == str.charAt(rightRide); /** * Check the rides are in Range */ const rideInRange = (str, leftRide, rightRide) => leftRide > 0 && rightRide < str.length; const longestSubstringPalindrome = str => { let longest = 0; let palindrome; for (let i = 1; i < str.length; i++) { let leftRide = i - 1; let rightRide = i + 1; // The left (i1) ride decreases one position while the right (i2) // increases one on each iteration. Iterate while the chars at i2 and i2 // are the same. Compare at any iteration if the lenth i2 - i1 becomes longer // than the previously marked as longest. In this case, update the longest and the // palindrome variable. Then increase the riding i's. while (charsAreEqual(str, leftRide, rightRide) && rideInRange(str, leftRide, rightRide)) { if (rightRide - leftRide > longest) { palindrome = str.substring(leftRide, rightRide+1); longest = rightRide - leftRide; } leftRide--; rightRide++; } } return palindrome; } longestSubstringPalindrome('aaababaacaacadaada'); }} - Javascripter October 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * Compare the chars at the given position are equal
 */
const charsAreEqual = (str, leftRide, rightRide) =>
  str.charAt(leftRide) == str.charAt(rightRide);

/**
 * Check the rides are in Range
 */
const rideInRange = (str, leftRide, rightRide) =>
  leftRide > 0 && rightRide < str.length;

const longestSubstringPalindrome = str => {
  let longest = 0;
  let palindrome;
  for (let i = 1; i < str.length; i++) {
    let leftRide = i - 1;
    let rightRide = i + 1;
    
    // The left (i1) ride decreases one position while the right (i2)
    // increases one on each iteration. Iterate while the chars at i2 and i2
    // are the same. Compare at any iteration if the lenth i2 - i1 becomes longer
    // than the previously marked as longest. In this case, update the longest and the
    // palindrome variable. Then increase the riding i's.
    while (charsAreEqual(str, leftRide, rightRide)
      && rideInRange(str, leftRide, rightRide)) {
      if (rightRide - leftRide > longest) {
        palindrome = str.substring(leftRide, rightRide+1);
        longest = rightRide - leftRide;
      }
      leftRide--;
      rightRide++;
    }
    
  }
  return palindrome;
}
  
longestSubstringPalindrome('aaababaacaacadaada');

- Javascripter October 14, 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