Interview Question for SDE-2s


Country: United States




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

None ever needed it, it is meaningless - but well, meaning is not what selling nowadays, snapchat is the proof. So here:

[ geeksforgeeks.org/manachers-algorithm-linear-time-longest-palindromic-substring-part-1 ]

That should solve it.

- NoOne June 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Naive solution in python. Cost (n^3). n^2 possible strings and the cost to check if each string is a palidrome is n.

def isPalindrome(s):
    return s == s[::-1]
                   
def longestPalindrome(s):
    max_length = 0
    for x in xrange(len(s)):
        for y in xrange(x+1, len(s)+1):
            if (y - x) > max_length and isPalindrome(s[x:y]):
                max_length = (y - x)
    return max_length

- Fernando June 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

lhjhjkhk

- Veerr June 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Of course these questions don't have values unless very specific scaled up things need some tuning. O(n2) is common implementation that works well. Here is O(n) implementation based on Manacher's algorithm for odd-length palindromes e.g. madam (can be extended to include even-length palindromes e.g. deed - more complex, but possible)

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>

using namespace std;

inline bool areEqualCharacters(char c1, char c2)
{
	// May be replaced with case-insensitive compare 
	if (tolower(c1) == tolower(c2))
		return true;
	return false;
}

int expand(string &s, size_t sLen, int c, int offset = 1)
{
	int left = c - offset;
	int right = c + offset;
	int start = left;
	// Assuming odd compare.
	// Logic can be expanded for palindrome of even length.
	while ((left >= 0) && (right < sLen))
	{
		if (!areEqualCharacters(s[left], s[right]))
		{
			break;
		}
		--left;
		++right;
	}
	if (left != start) {
		++left;
		--right;
		return (right - left + 1);
	}
	return 0; // Unable to expand
}

string findLargestPalindrome(string& s)
{
	size_t sLen = s.length();
	vector<int> expansion(sLen);
	multimap<int, int> sortedExpansion;
	for (int center = 0; center < sLen;)
	{
		int expandedRange = expand(s, sLen, center);
		expansion[center] = expandedRange;
		sortedExpansion.emplace(expandedRange, center);
		if (expandedRange > 0)
		{
			// Mirror right and select next center
			int halfRange = expandedRange / 2;
			int mirrorValue, rightIndex = 0, largestRight = 0;
			while (halfRange > 0) {
				mirrorValue = expansion[center - halfRange];
				rightIndex = center + halfRange;
				expansion[rightIndex] = mirrorValue;
				sortedExpansion.emplace(mirrorValue, rightIndex);
				if (expansion[largestRight] < mirrorValue)
					largestRight = rightIndex;
				--halfRange;
			}
			// Move center to next possible largest palindrome
			// or beyond current mirrored right boundary
			center = (0 != largestRight) ? largestRight: (rightIndex + 1);
		}
		else
			++center;
	}
	auto largestString = sortedExpansion.rbegin();
	if (1 < largestString->first)
	{
		pair<int, int> p = *largestString;
		string largestPalindromeSub = s.substr(largestString->second - (largestString->first/2), largestString->first);
		return largestPalindromeSub;
	}
	return "";	// No palindrome sub-string found.
}

int main()
{
	//string s = "abracadabra";
	string s = "aaaomkaraKmoa";
	string lp = findLargestPalindrome(s);
	return 0;
}

- Omkar June 22, 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