makemytrip Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
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
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
}
}
}
#include <bits/stdc++.h>
using namespace std;
void solver(string s1, string s2){
int n=s1.size();
int m=s2.size();
if(m>n){
cout<<"-1";
return;
}
string resStr="";
int res=INT_MAX;
for(int i=0;i<=n-m;i++){
string temp=s1.substr(0,i)+s2+s1.substr(i+m);
int operation=0;
for(int j=i;j<i+m;j++){
if(temp[j]!=s1[j]){
operation++;
}
}
bool isPossible=true;
for(int j=0;j<(n+1)/2;j++){
if((j<i || n-j-1>=i+m) && temp[j]!=temp[n-j-1]){
/* atleast one word should be outside of the substring range and it should be of same character,
so change the character which is outside of range with the inside character */
if(j<i){
temp[j]=temp[n-j-1];
}
else{
temp[n-j-1]=temp[j];
}
operation++;
}
else if(temp[j]!=temp[n-j-1]){
/* this means both start and end index are now in range of substring so if the substring itself
is not making palindrome, just break out, since we cannot change the substring as we need it compulsarily */
isPossible=false;
break;
}
}
if(isPossible==true){
if(res>operation){
resStr=temp;
res=operation;
}
}
}
if(res==INT_MAX){
cout<<"-1";
return;
}
cout<<res<<" "<<resStr<<endl;
}
int main(){
string word[]={"abbd","aaaaa","abcd"};
string prev_word[]={"mr","bbb","zc"};
for(int i=0;i<3;i++){
solver(word[i],prev_word[i]);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void solver(string s1, string s2){
int n=s1.size();
int m=s2.size();
if(m>n){
cout<<"-1";
return;
}
string resStr="";
int res=INT_MAX;
for(int i=0;i<=n-m;i++){
string temp=s1.substr(0,i)+s2+s1.substr(i+m);
int operation=0;
for(int j=i;j<i+m;j++){
if(temp[j]!=s1[j]){
operation++;
}
}
bool isPossible=true;
for(int j=0;j<(n+1)/2;j++){
if((j<i || n-j-1>=i+m) && temp[j]!=temp[n-j-1]){
/* atleast one word should be outside of the substring range and it should be of same character,
so change the character which is outside of range with the inside character */
if(j<i){
temp[j]=temp[n-j-1];
}
else{
temp[n-j-1]=temp[j];
}
operation++;
}
else if(temp[j]!=temp[n-j-1]){
/* this means both start and end index are now in range of substring so if the substring itself
is not making palindrome, just break out, since we cannot change the substring as we need it compulsarily */
isPossible=false;
break;
}
}
if(isPossible==true){
if(res>operation){
resStr=temp;
res=operation;
}
}
}
if(res==INT_MAX){
cout<<"-1";
return;
}
cout<<res<<" "<<resStr<<endl;
}
int main(){
string word[]={"abbd","aaaaa","abcd"};
string prev_word[]={"mr","bbb","zc"};
for(int i=0;i<3;i++){
solver(word[i],prev_word[i]);
}
return 0;
}
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:
- Anonymous May 06, 2018