Booking.com Interview Question for Software Engineers


Country: Netherland
Interview Type: Phone Interview




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

Java using stack:

import java.util.Stack;

public class RemoveRepetitive
{
    public static void printUnique(String input)
    {
        Stack<Character> stack = new Stack<>();
        for (int index = 0; index < input.length(); index++)
        {
            if (!stack.isEmpty() && stack.peek() == input.charAt(index))
            {
                continue;
            }
            else
            {
                stack.push(input.charAt(index));
            }
        }
        System.out.println(stack.toString());
    }

    public static void main(String[] args)
    {
        printUnique("abababaabbbaaaa");
    }
}

- Neo February 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

def compress(string):                                                           
    prev = None                                                                 
    cons = 1                                                                    
    compressed = ''                                                             
    for i in string:                                                            
        if prev:                                                                
            if prev == i:                                                       
                cons += 1                                                       
            else:                                                               
                compressed += prev+str(cons)                                    
                cons = 1                                                        
        prev = i                                                                
    compressed += prev+str(cons)                                                
                                                                                
    return compressed

- anant September 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Simple and effective Java solution using while loop:

public String charFrequency(String s) {
    String res = "";
    int ind = 0;
    while (ind < s.length()) {
        char cur = s.charAt(ind);
        int num = 1;
        while (ind < s.length() - 1 && cur == s.charAt(ind + 1)) {
            ind++;
            num++;
        }
        ind++;
        res += String.format("%s%s", cur, num);
    }
    return res;
}

- megido December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Sorry, Ignore my last comment. It was my first answer in careercup. It was showing error message, so I have added comment in all lines.
Please use this one:

my $str = 'abababaabbbaaaaa';
# a1b1a1b1a1b1a2b3a5
my @str = split '', $str;

my $op = '';
my $cnt = 1;
for my $i (0 .. length($str)) {
    if ($str[$i] eq $str[$i+1]) {
      $cnt++;
    } else {
      $op .= $str[$i].$cnt;
      $cnt = 1; 
    }
}
print $op;

- Kamal Nayan December 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def compress(string):                                                           
    prev = None                                                                 
    cons = 1                                                                    
    compressed = ''                                                             
    for i in string:                                                            
        if prev:                                                                
            if prev == i:                                                       
                cons += 1                                                       
            else:                                                               
                compressed += prev+str(cons)                                    
                cons = 1                                                        
        prev = i                                                                
    compressed += prev+str(cons)                                                
                                                                                
    return compressed

- anant September 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ruby

def compressed(input)
  reps = []
  input.chars.each do |c|
    if reps[-2] == c
      reps[-1] += 1
    else
      reps << c << 1
    end
  end 
  reps * ""
end

- fl00r January 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def compress(word):
dic_letter = {}
for letter in word:
if(dic_letter.get(letter) == None):
dic_letter[letter] = 0
else:
dic_letter[letter] = dic_letter[letter] + 1
return dic_letter

print compress("asdasdssss")

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

def compress(word):
	dic_letter = {}
	for letter in word:
		if(dic_letter.get(letter) == None):
			dic_letter[letter] = 0
		else:
			dic_letter[letter] = dic_letter[letter] + 1
	return dic_letter

print compress("asdasdssss")

- foomaster March 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is simple solution in perl:

my $str = 'abababaabbbaaaaa';

my @str = split '', $str;

my $op = '';

my $cnt = 1;

for my $i (0 .. length($str)) {

if ($str[$i] eq $str[$i+1]) {

$cnt++;

} else {

$op .= $str[$i].$cnt;

$cnt = 1;

}

}

print $op;

- Kamal Nayan December 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdlib.h>

#define ARRAY_LENGTH 26

using namespace std;

int charArray[ARRAY_LENGTH] = {0};

int Solve(char* pSouce);
int Print();
void Reset();


void Reset(){ for ( int i = 0; i< ARRAY_LENGTH;i++){ charArray[i] = 0; } }

int Print(){
cout << endl;
char ch = 'a';
for ( int i = 0; i< ARRAY_LENGTH;i++){ cout <<"[" << (char(ch + i)) <<"]:"<< charArray[i] << endl; }
}

int Solve(string pSource){
if ( pSource.length() == 0 ){ return 0; }
for(int i = 0; i< pSource.length();i++){ //Assuming input string is NULL terminated.
int index = 0;
index = abs(pSource.at(i) - 'a');

if ( charArray[index] != 0 ){
charArray[index] +=1;
} else {
charArray[index] = 1;
}
}
return 1;
}



int main(){
string inputStr;
int test_cases = 0;
cin >> test_cases;
for ( int i = 0; i< test_cases; i++){
cin >> inputStr;
cout <<inputStr.c_str();
Solve(inputStr);
Print();
Reset();
}
}

- Rakesh January 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdlib.h>

#define ARRAY_LENGTH 26

using namespace std;

int charArray[ARRAY_LENGTH] = {0};

int Solve(char* pSouce);
int Print();
void Reset();


void Reset(){ for ( int i = 0; i< ARRAY_LENGTH;i++){ charArray[i] = 0; } }

int Print(){
	cout << endl;
	char ch = 'a';
	for ( int i = 0; i< ARRAY_LENGTH;i++){  cout <<"[" << (char(ch + i)) <<"]:"<< charArray[i] << endl; }
}

int Solve(string pSource){
	if ( pSource.length() == 0 ){  return 0; }
	for(int i = 0; i< pSource.length();i++){				//Assuming input string is NULL terminated.
		int index = 0;
		index = abs(pSource.at(i) - 'a');

		if ( charArray[index] != 0 ){
			charArray[index] +=1;
		} else {
			charArray[index] = 1;
		}
	}
	return 1;
}



int main(){
	string inputStr;
	int test_cases = 0;
	cin >> test_cases;
	for ( int i = 0; i< test_cases; i++){
		cin >> inputStr;
		cout <<inputStr.c_str();
		Solve(inputStr);
		Print();
		Reset();
	}
}

- Rakesh January 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If required as follows
input: aabbbbaab;
output: abab



public class RemoveDuplicates {
public static void main(String[] args) {
RemoveDuplicates dup = new RemoveDuplicates();
System.out.println(dup.compress("aabbbbbaab", 1));
}

private String compress(String string, int i) {
// TODO Auto-generated method stub
int start = 0;
char c = string.charAt(start);
String result = "";
while(i < string.length()) {
if(string.charAt(i) == c) {
i++;
} else {
result += c;
c = string.charAt(i);
i++;
}
}
result += c;
return result;
}
}

- imran sarwar January 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RemoveDuplicates {
public static void main(String[] args) {
RemoveDuplicates dup = new RemoveDuplicates();
System.out.println(dup.compress("aabbbbbaab", 1));
}

private String compress(String string, int i) {
// TODO Auto-generated method stub
int start = 0;
char c = string.charAt(start);
String result = "";
while(i < string.length()) {
if(string.charAt(i) == c) {
i++;
} else {
result += c;
c = string.charAt(i);
i++;
}
}
result += c;
return result;
}
}

- imran sarwar January 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Repeat {
	public static void main (String args[]) {
		System.out.println(new Repeat().compress("abababaabbbaaaaa"));
	}
	public String compress(String string) {
		if (string.length() == 0)
			return "";
		return compress(string, string.charAt(0), 0);
	}
	private String compress(String string, char c, int count) {
		if (string.length() == 0)
			return c+""+count+"";
		if (string.charAt(0) == c) {
			return compress(string.substring(1), c, count + 1);
		}else{
			if (count == 0)
				return c+""+compress(string.substring(1));
			return c+""+count+compress(string);
		}
	}
}

- w.kinaan July 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void charFrequency() {
		String s = "aabbbcaghgggfg fhggssssy";
		String res = "";
		int cons = 1, i=0;
		boolean flag = false;
		if(s.length()==0)
		{
			res="Invalid Input";
		}
		else
		{
			for(i=0;i<s.length()-1;i++)
			{
				if(s.charAt(i)==s.charAt(i+1))
				{
					cons++;
					flag = true;
				}
				else
				{
					if(flag)
					{
						res+=s.charAt(i)+String.valueOf(cons);
						flag = false;
					}
					else
					{
						res+=s.charAt(i)+String.valueOf(cons);
					}
					cons=1;
				}

			}
			res+=s.charAt(i)+String.valueOf(cons);
			System.out.println(res);
		}
	}

- Nitesh August 10, 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