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

Are you sure string should be aabcbcbcdddd or (aabcccdddd)?

- Himanshu Jain January 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is aabcbcbcdddd

- kumarraju05 January 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your understanding "aabcccdddd", in this context, should be a2b(1)c3d4

- gameboy1024 January 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I did a parsing of the string.

def process(s):
    info = []
    pos = 0
    word = ''
    num = ''
    for i in s:
        if i.isdigit():
            info.append(pos)
            info.append(word) 
            num += i
        elif i.isalpha():
            if num != '':
                pos += int(num)*len(word)
                num = ''
                word = ''
            word += i
    return info

def get(index, info):
    if index < 0:
        raise
    pos = 0
    for i in xrange(0, len(info), 2):
        if info[i] > index:
            pos = info[i-2]
            break

    return info[pos+1][(index-pos)%len(info[pos+1])]

if __name__ == '__main__':

    s = "a2bc3d4"
    print get(7, process(s))

- jilinxie1988@gmail.com January 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public char charAt(String s , int position){
        int index = 0;
        int i = position;
        while (index < s.length()){
            String letters = getNextLetters(s , index);
            index += letters.length();
            int count = getNextNumber(s , index);
            index += String.valueOf(count).length();
            while (count > 0){
                if (i < letters.length()){
                    return letters.charAt(i);
                }
                i -= letters.length();
                count--;
            }
        }
        return ' ';
    }

    private String getNextLetters(String s , int index){
        StringBuilder sb = new StringBuilder();
        while(index < s.length() && !isNumber(s.charAt(index))){
            sb.append(s.charAt(index));
            index++;
        }
        return sb.toString();
    }

    private int getNextNumber(String s, int index){
        if (index >= s.length()) {
            return 1;
        }
        int result = 0;
        while(index < s.length() && isNumber(s.charAt(index))){
            result = result * 10 + (s.charAt(index) - '0');
            index++;
        }
        return result;
    }

    private boolean isNumber(char c){
        return c >= '0' && c <= '9';
    }

- GK January 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
char str[100];
scanf("%s",str);
int l=strlen(str);
int k=0,i,j,c;
int index=7;
for(i=0;i<l;)
{
j=i;
if(str[i]>='a' && str[i]<='z')
{
c=0;
while(str[i]>='a' && str[i]<='z')
{c++;
i++;
printf("c: %d i: %d\n",c,i);
}
k=str[i]-'0';
i++;
printf("%d %d\n",k,i);
//char *str1=(char*)malloc(sizeof(char)*c);
k=k*c;
printf(" kc: %d",k);
int m,x=0;
if(k<index)
{index=index-k;
printf(" index %d\n",index);}
else
{
char *str1=(char*)malloc(sizeof(char)*c);
for(m=j;m<i-1;m++)
str1[x++]=str[m];
str1[x]='\0';
printf("%s",str1);
int a=0;
for(j=0;j<index;)
{
a=0;
for(;str1[a] && j<index;)
{
a++;
j++;
}
}
printf("%c",str1[a-1]);
break;
}
}

}
}

- Anonymous January 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public char getChar (String src , int index){
    	int j = 0 ;
    	int l = 0 , sum = 0 ,total = 0;    	
    	for (int i = 0 ; i < src.length() ;++i) {
    		sum = 0;
    		l = 0 ;
    		while (i < src.length() && Character.isDigit(src.charAt(i))) {
    			sum = sum * 10 + (src.charAt(i) - '0') ;
    			l++;
    			i++;
    		}
    		if (sum > 0) {
    			i--;
        		int len = i - l - j + 1;
        		total += len * sum;
                if (index <= total + 1) {
        			int step = (index - j) % len ;
        		    return src.charAt(j + step);        		
        		}
        		j = i + 1 ;        	 
    		}
    	}
    	return ' ';

}

- Scott January 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	
	public static void findCharAtInEncodedString(String st,int index)
	{
		char s[] = st.toCharArray();
		int prePos=0,currPos=0;
		int totalVal =0;
		boolean isFound = false;
		
		for(int i=0;i<s.length;i++)
		{
			int val = s[i] -'0';
			if(val>0 && val <=9)
			{
				currPos = i;
				totalVal +=val;
				
				if(prePos == 0)
				{
					prePos = currPos;
					index = (prePos*val < index ?(index - prePos*val):(prePos*val - index));
				}
				else
				{
					int ind = currPos - prePos -1;
					if(index < ind*val)
					{
						while(ind <= index)
						{
					   		index = index -ind;
						}
						
						 
						totalVal = totalVal+index - val;
						isFound = true;
					}
					else
					{
						index = index - ind*val;
						prePos = currPos;
					}
				}
				
			}
			
			if(isFound)
			{
				
				System.out.println(" "+s[totalVal]);
				break;
			}
			
			
		}
		
	}
	
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		 findCharAtInEncodedString("a2bc3d4",7);
		 findCharAtInEncodedString("a2bc3d4",6);
		 findCharAtInEncodedString("a2bc3d4",5);
		 findCharAtInEncodedString("a2bc3d4",4);
		 findCharAtInEncodedString("a2bc3d4",8);
		 findCharAtInEncodedString("a2bc3d4",9);
	}
}

- Himanshu Jain January 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * @author Gourav.
 *
 */
public class Demo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(getCharFromEncodedString("a2bc3d4",7));
	}
	
	public static char getCharFromEncodedString(String s, int index){
		
		char[] arr = s.toCharArray();
		
		int previousNumberIndex = -1;
		StringBuffer sb = new StringBuffer();
		
		for (int i=0;i<arr.length;i++) {
			if(Character.isDigit(arr[i])){
				int j=0;
				int repeatCount = Character.digit(arr[i], Character.MAX_RADIX);
				String repeatStr = s.substring(previousNumberIndex+1, i);
				for( ; j < repeatCount; j++){
					sb.append(repeatStr);
				}
					
				previousNumberIndex = i;
			} 
		}
		
		return sb.charAt(index);
	}

}

- gouravsood January 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String foo(String encoded, int index) {
// next number
int next = 0;
// current digit
int start = -1;
// number of letters between the two adjacent digit
int span = 0;
// current total letters
int total = 0;

for (int i = 0; i < encoded.length(); i++) {
// if the char is digit
if (Character.isDigit(encoded.charAt(i))) {
next = Character.getNumericValue(encoded.charAt(i));

span = (i - start) - 1;

total += span * next;

if (total <= index) {
start = i;
continue;
} else {
int which = total - index;
return encoded.substring(start + 1 + (which % span), start
+ 1 + (which % span) + 1);
}

}

}
if (index >= total) {
System.out.println("Sorry, the index is out of bound");
}

return null;
}
}

- Boge February 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String foo(String encoded, int index) {
// next number
int next = 0;
// current digit
int start = -1;
// number of letters between the two adjacent digit
int span = 0;
// current total letters
int total = 0;

for (int i = 0; i < encoded.length(); i++) {
// if the char is digit
if (Character.isDigit(encoded.charAt(i))) {
next = Character.getNumericValue(encoded.charAt(i));

span = (i - start) - 1;

total += span * next;

if (total <= index) {
start = i;
continue;
} else {
int which = total - index;
return encoded.substring(start + 1 + (which % span), start
+ 1 + (which % span) + 1);
}

}

}
if (index >= total) {
System.out.println("Sorry, the index is out of bound");
}

return null;
}
}

- Boge February 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str="a2bc3d4";

char[] chr=str.toCharArray();
int index=7;
int m=0;
StringBuffer sb=new StringBuffer();

for(int i=0;i<chr.length;i++){
if(Character.isDigit(chr[i])){

for(int j=0;j<Integer.parseInt(Character.toString(chr[i]));j++){
sb.append(str.substring(m,i));
}
m=i+1;
}



}
System.out.println(+sb.charAt(index));

}

- TestCode February 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
String string = "a2bc3d4";
int index =7;
char[] arr = string.toCharArray();
int prevDigitIndex = 0;
int stringSize = 0;
int lengthOfOrgString = 0;
for(int i=0;i< arr.length; i++){
if(Character.isDigit(arr[i])){
int lastlengthOfString = lengthOfOrgString;
lengthOfOrgString = stringSize*Character.getNumericValue(arr[i]) + lengthOfOrgString;
if(index <= lengthOfOrgString-1){
String subString = string.substring(prevDigitIndex+1, i);
int pos = (index-lastlengthOfString)%(subString.length());
System.out.println(subString.charAt(pos));
break;
}
stringSize = 0;
prevDigitIndex = i;
} else{
stringSize++;
}
}
}

- Sachin July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
String string = "a2bc3d4";
int index =7;
char[] arr = string.toCharArray();
int prevDigitIndex = 0;
int stringSize = 0;
int lengthOfOrgString = 0;
for(int i=0;i< arr.length; i++){
if(Character.isDigit(arr[i])){
int lastlengthOfString = lengthOfOrgString;
lengthOfOrgString = stringSize*Character.getNumericValue(arr[i]) + lengthOfOrgString;
if(index <= lengthOfOrgString-1){
String subString = string.substring(prevDigitIndex+1, i);
int pos = (index-lastlengthOfString)%(subString.length());
System.out.println(subString.charAt(pos));
break;
}
stringSize = 0;
prevDigitIndex = i;
} else{
stringSize++;
}
}
}

- Sachin July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
        {
          char[] array =  DecodeString("a2bc3d4").ToCharArray();
          Console.WriteLine(array[7]);
          Console.ReadLine();
        }

        private static string DecodeString(string s)
        {
            int charIndex = -1;
            StringBuilder sb = new StringBuilder();
            char[] arr = s.ToCharArray();
            string temp = string.Empty;
            string result = string.Empty;
            int repeater;
            for (int i = 0; i < arr.Length; i++)
            {
                
                if (arr[i].isDigit(out repeater))
                {
                    string tempResult = string.Empty;
                    for (int j = 1; j <= repeater; j++)
                    {
                        tempResult += temp;
                    }
                    temp = string.Empty;
                    result += tempResult;
                }
                else
                {
                    
                    temp += arr[i].ToString();
                }

              

            }

            return result;

            
        }

       
    }
    static class Extension
    {
       public static bool isDigit(this char c, out int repeater)
       {

           return Int32.TryParse(c.ToString(), out repeater);
       }

}}

- kapil July 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
        {
          char[] array =  DecodeString("a2bc3d4").ToCharArray();
          Console.WriteLine(array[7]);
          Console.ReadLine();
        }

        private static string DecodeString(string s)
        {
            int charIndex = -1;
            StringBuilder sb = new StringBuilder();
            char[] arr = s.ToCharArray();
            string temp = string.Empty;
            string result = string.Empty;
            int repeater;
            for (int i = 0; i < arr.Length; i++)
            {
                
                if (arr[i].isDigit(out repeater))
                {
                    string tempResult = string.Empty;
                    for (int j = 1; j <= repeater; j++)
                    {
                        tempResult += temp;
                    }
                    temp = string.Empty;
                    result += tempResult;
                }
                else
                {
                    
                    temp += arr[i].ToString();
                }

              

            }

            return result;

            
        }

       
    }
    static class Extension
    {
       public static bool isDigit(this char c, out int repeater)
       {

           return Int32.TryParse(c.ToString(), out repeater);
       }
    }

- kapil July 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
{
char[] array = DecodeString("a2bc3d4").ToCharArray();
Console.WriteLine(array[7]);
Console.ReadLine();
}

private static string DecodeString(string s)
{
int charIndex = -1;
StringBuilder sb = new StringBuilder();
char[] arr = s.ToCharArray();
string temp = string.Empty;
string result = string.Empty;
int repeater;
for (int i = 0; i < arr.Length; i++)
{

if (arr[i].isDigit(out repeater))
{
string tempResult = string.Empty;
for (int j = 1; j <= repeater; j++)
{
tempResult += temp;
}
temp = string.Empty;
result += tempResult;
}
else
{

temp += arr[i].ToString();
}



}

return result;


}


}
static class Extension
{
public static bool isDigit(this char c, out int repeater)
{

return Int32.TryParse(c.ToString(), out repeater);
}
}

- kapil July 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int N = sc.nextInt();
		char[] arr = s.toCharArray();
		String temp = "";
		String finalS = "";

		for (int i = 0; i < arr.length; i++) {
			if (Character.isDigit(arr[i])) {
				for (int j = 0; j < Integer.parseInt(String.valueOf(arr[i])); j++) {
					finalS += temp;
				}
				temp = "";
			} else {
				temp += String.valueOf(arr[i]);
			}
		}
		if (N < finalS.length())
			System.out.println(finalS.charAt(N));
	}

}

- Khushboo Agrawal April 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int N = sc.nextInt();
		char[] arr = s.toCharArray();
		String temp = "";
		String finalS = "";

		for (int i = 0; i < arr.length; i++) {
			if (Character.isDigit(arr[i])) {
				for (int j = 0; j < Integer.parseInt(String.valueOf(arr[i])); j++) {
					finalS += temp;
				}
				temp = "";
			} else {
				temp += String.valueOf(arr[i]);
			}
		}
		if (N < finalS.length())
			System.out.println(finalS.charAt(N));
	}

}

- Khushboo Agrawal April 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char func(string enc, int index){

char *str = new char[enc.length() + 1];
int i = 0, num = 0, flag = 0;

for(int j = 0; j<enc.length(); j++){
if(enc.at(j) >= '0' && enc.at(j) <= '9'){
num = num * 10 + enc.at(j) - '0';
flag = 2;
}
else if(flag == 2){
if( (index+1) <= num * i ){
if( (index+1) % i == 0 )
return str[i-1];
return str[ ((index+1)%i) - 1 ];
}
index -= num*i;
num = i = flag = 0;
str[i++] = enc.at(j);
}
else{
str[i++] = enc.at(j);
}
}

if( (index+1) <= num * i ){
if( (index+1) % i == 0 )
return str[i-1];
return str[ ((index+1)%i) - 1 ];
}

return NULL;
}

- xcode May 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char func(string enc, int index){
    
    char *str = new char[enc.length() + 1];
    int i = 0, num = 0, flag = 0;
    
    for(int j = 0; j<enc.length(); j++){
        if(enc.at(j) >= '0' && enc.at(j) <= '9'){
            num = num * 10 + enc.at(j) - '0';
            flag = 2;
        }
        else if(flag == 2){
            if( (index+1) <= num * i ){
                if( (index+1) % i == 0 )
                    return str[i-1];
                return str[ ((index+1)%i) - 1 ];
            }
            index -= num*i;
            num = i = flag = 0;
            str[i++] = enc.at(j);
        }
        else{
            str[i++] = enc.at(j);
        }
    }
    
    if( (index+1) <= num * i ){
        if( (index+1) % i == 0 )
            return str[i-1];
        return str[ ((index+1)%i) - 1 ];
    }
    
    return NULL;

}

- xcode May 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char func(string enc, int index){
    
    char *str = new char[enc.length() + 1];
    int i = 0, num = 0, flag = 0;
    
    for(int j = 0; j<enc.length(); j++){
        if(enc.at(j) >= '0' && enc.at(j) <= '9'){
            num = num * 10 + enc.at(j) - '0';
            flag = 2;
        }
        else if(flag == 2){
            if( (index+1) <= num * i ){
                if( (index+1) % i == 0 )
                    return str[i-1];
                return str[ ((index+1)%i) - 1 ];
            }
            index -= num*i;
            num = i = flag = 0;
            str[i++] = enc.at(j);
        }
        else{
            str[i++] = enc.at(j);
        }
    }
    
    if( (index+1) <= num * i ){
        if( (index+1) % i == 0 )
            return str[i-1];
        return str[ ((index+1)%i) - 1 ];
    }
    
    return NULL;
}

- xcode May 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>
using namespace std;

int main() {
int i = 0, k=0, prev_index = -1, prev_sum = 0, c=0, curr_index = -1, curr_sum = 0;
string str = "abcd2bc4d3e2";
cout << " str is : " << str << endl;
cout << " Enter k : " ;
cin >> k;

while(i < str.size() ) {
if(str[i] >= '0' && str[i] <= '9') {
prev_index = curr_index;
prev_sum = curr_sum;
curr_index = i;
curr_sum = curr_sum + (c * (str[i] - '0'));
c=0;
}
else
c++;

if(k == 0) {
cout << str[0] << endl;
break;
}
else if( prev_index == -1 && k < curr_sum) {
k = k - prev_sum;
k = k%curr_index;
break;
}
else if( k >= prev_sum && k < curr_sum && prev_sum != curr_sum) {
if( k == prev_sum )
break;
if( k == curr_sum )
break;
k = k - prev_sum;
k = k % (curr_index - prev_index - 1);
break;
}
i++;
}
return 0;
}

- kbkunalb June 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>
using namespace std;

int main() {
int i = 0, k=0, prev_index = -1, prev_sum = 0, c=0, curr_index = -1, curr_sum = 0;
string str = "abcd2bc4d3e2";
cout << " str is : " << str << endl;
cout << " Enter k : " ;
cin >> k;

while(i < str.size() ) {
if(str[i] >= '0' && str[i] <= '9') {
prev_index = curr_index;
prev_sum = curr_sum;
curr_index = i;
curr_sum = curr_sum + (c * (str[i] - '0'));
c=0;
}
else
c++;

if(k == 0) {
cout << str[0] << endl;
break;
}
else if( prev_index == -1 && k < curr_sum) {
k = k - prev_sum;
k = k%curr_index;
cout << " element is : " << str[k] << endl;;
break;
}
else if( k >= prev_sum && k < curr_sum && prev_sum != curr_sum) {
if( k == prev_sum ) {
cout << " element is : " << str[prev_index+1] << endl;
break;
}
if( k == curr_sum ) {
cout << " element is : " << str[i+1] << endl;
break;
}
k = k - prev_sum;
k = k % (curr_index - prev_index - 1);
cout << " element is : " << str[prev_index + k + 1] << endl;;
break;
}
i++;
}
return 0;
}

- kbkunalb June 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string="a2bc3d4"
substr=""
print string
index=input('Enter a index : ')
for i in string:
if i.isalpha():
substr+=i
elif i.isdigit():
if len(substr)*int(i)<index:
index=index-(len(substr)*int(i))
substr=""
elif len(substr)*int(i)>=index:
print substr[(index%int(i))-1]
break
else:
print "Index out of range..."

- Chandan June 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string="a2bc3d4"
substr=""
print string
index=input('Enter a index : ')
for i in string:
    if i.isalpha():
        substr+=i
    elif i.isdigit():
        if len(substr)*int(i)<index:
            index=index-(len(substr)*int(i))
            substr=""
        elif len(substr)*int(i)>=index:
            print substr[(index%int(i))-1]
            break
else:
    print "Index out of range..."

- Anonymous June 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string="a2bc3d4"
substr=""
print string
index=input('Enter a index : ')
for i in string:
    if i.isalpha():
        substr+=i
    elif i.isdigit():
        if len(substr)*int(i)<index:
            index=index-(len(substr)*int(i))
            substr=""
        elif len(substr)*int(i)>=index:
            print substr[(index%int(i))-1]
            break
else:
    print "Index out of range..."

- Chandan June 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def main(args:Array[String]) ={
    println(decodePosition("a2bc3d4", 7))
    println(decodePosition("a2bc3d4", 10))
  }
  def decodePosition(s:String, pos:Int): Any = {
    var de = ""
    var repeatString = ""
    for(i<-0 to s.length-1) {
      if(s(i).isLetter == true) {
        repeatString += s(i)
      } else {
        if(s(i).isDigit == true) {
          for(j<-0 to s(i).asDigit-1) {
            de += repeatString
          }
          repeatString = ""
        }
      }
      if(de.length > pos) {
        return de(pos-1)
      }
    }
    if(de.length < pos)
      -1

  }

}

- san October 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const str = 'a2bc3f4';
const indexAt = 7;

function search(str, index) {
  let word = '';
  let temp = '';
  for(let i = 0; i < str.length; i++) {
    if(/\d/.test(str[i])) {
      word += temp.repeat(Number(str[i]))
      temp = ''
    } else {
      temp += str[i];
    }
  }
  return word.charAt(index);
};

console.log(search(str, indexAt));

- Kanika Rungta January 07, 2022 | 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