## 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 =  * 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``````

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 =  * 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``````

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

``````import scala.collection.mutable.Map
object Mmt1 {
def main(args:Array[String]) = {
println(findPalindrome("babammm",2))
println(findPalindrome("babammm",5))
}
def findPalindrome(s:String, c:Int): String = {
for(i<-0 to s.length-1-c) {
if(palindrom(s.substring(i,c +i))){
return s.substring(i,c+i)
}
}
""
}
def palindrom(s:String):Boolean ={
var hash:Map[Char,Int] = Map()
for(i<-s) {
if(!hash.contains(i)){
hash += i -> 1
} else {
var x = hash(i)
hash(i) = (x + 1)
}
}
var odd = 0
var even = 0
for(i<-hash){
if(i._2 % 2 != 0 ){
odd += 1
}
}
if (odd == 1 || odd == 0){
true
} else{
false
}
}
}``````

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.

### 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.