makemytrip Interview Question for Java Developers


Country: India
Interview Type: Written Test




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

I ignored the part with T inputs, and just considered the case where we have S and K.

The general process is like this:
1. Find all subsets of length K in S.
2. For each subset, count all characters. If only one char count in the substring is odd, a palindrome can be formed. Return K.
3. If |S| == K, return -1
4. Repeat the process for S and K+1.

Solution in Python:

def candy_pal(str,k):
    substrings = []
    for i in range(len(str) - k + 1):
        substrings.append(str[i:i+k])

    for substring in substrings:
        if has_pal(substring):
            return k

    if len(str) == k:
        return -1

    return candy_pal(str,k+1)

def has_pal(str):
    char_count = [0] * 26
    for char in str:
        char_ord = ord(char) - ord('a')
        char_count[char_ord] = char_count[char_ord] + 1

    has_odd = False
    for i in range(len(char_count)):
        if char_count[i] % 2 == 1:
            if has_odd:
                return False
            else:
                has_odd = True

    return True

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

I ignored the part where there are T inputs of K. This part depends on the format of the input (text file, variadic function, array, etc.) and should be discussed with the interviewer.

The general process is as follows:
1. Find all substrings of length K in S.
2. For each substring, count the characters. If only one character count is odd, a palindrome can be formed. Return K.
3. If |S| == K, return -1 (no palindrome of length K or greater can be found)
4. Repeat the process for S and K+1.

def candy_pal(str,k):
    substrings = []
    for i in range(len(str) - k + 1):
        substrings.append(str[i:i+k])

    for substring in substrings:
        if has_pal(substring):
            return k

    if len(str) == k:
        return -1

    return candy_pal(str,k+1)

def has_pal(str):
    char_count = [0] * 26
    for char in str:
        char_ord = ord(char) - ord('a')
        char_count[char_ord] = char_count[char_ord] + 1

    has_odd = False
    for i in range(len(char_count)):
        if char_count[i] % 2 == 1:
            if has_odd:
                return False
            else:
                has_odd = True

    return True

- Anonymous May 06, 2018 | 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