Expedia Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

/* 
There are three variations of the problem, 
when I used to ask this.
1. A string is actually a sequence of chars.
   Those makes whitespaces special.
   1.1 'I am   cool' --> 'cool   am I'
   1.2 'cool am   I' 
2. 2.1 'cool am I'
People generally tend to solve the 2.1.
While, 1.1, and 1.2 are where the fun really is.
Now, I present 1 liners, fully declarative form.
*/
// 2.1. The easiest one
str( (tokens( string , '\\S+') ** -1 ), ' ')
/* 
Blue is Sky The // String
*/
// 1.1 :: 'I am   cool'
spaces = tokens( string , '\\s+') 
words = tokens( string , '\\S+')
l = fold ( [size(spaces)-1:-1 ] , list(words[-1]) ) as { $.p += (spaces[$.o] + words[$.o]) } 
str(l,'')
// 1.2 trivial. left as exercise .

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

#include<stdio.h>
#include<string.h>
 
 void main(){
 	int i,j,k,l,temp,x,p=0;
 	char a[100];
 	gets(a);
 	l=strlen(a);
 	j=l-1;
 	for(i=0;i<j;i++){
 		temp=a[i];
 		a[i]=a[j];
 		a[j]=temp;-
 		j--;
	 }
 	
 
 	a[l]=' ';
 	
 	for(i=0;i<l+1;i++){
 		if(a[i]==' '){
 			k=i-1;
 			for(x=p;x<k;x++){
 		temp=a[x];
 		a[x]=a[k];
 		a[k]=temp;
 		k--;
	 }
 		p=i+1;
		 }
	 }
	 
	 puts(a);

 }

- Shubham Jain July 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseWords {
	public static void main(String[] args) {
		System.out.println(reverseWords("The Sky is Blue"));
		System.out.println(reverseWords("My name is Bushan"));
	}
	
	public static String reverseWords(String string){
		String words[] = string.split(" ");
		int last = words.length - 1;
		for(int first = 0; first < last; first++, last--){
			String temp = words[first];
			words[first] = words[last];
			words[last] = temp;
		}
		//In java-8
		String str = String.join(" ", words);
		return str;
	}
}	
/*
Input: The Sky is Blue
Output: Blue is Sky The

Input: My name is Bushan
Output: Bushan is name My
*/

- Bushan SC July 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u plz explain this in a easy way

- adithya January 11, 2024 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
 
 void main(){
 	int i,j,k,l,temp,x,p=0;
 	char a[100];
 	gets(a);
 	l=strlen(a);
 	j=l-1;
 	for(i=0;i<j;i++){
 		temp=a[i];
 		a[i]=a[j];
 		a[j]=temp;-
 		j--;
	 }
 	
 
 	a[l]=' ';
 	
 	for(i=0;i<l+1;i++){
 		if(a[i]==' '){
 			k=i-1;
 			for(x=p;x<k;x++){
 		temp=a[x];
 		a[x]=a[k];
 		a[k]=temp;
 		k--;
	 }
 		p=i+1;
		 }
	 }
	 
	 puts(a);

 }

- Shubham Jain July 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

void main(){
	int n,arr[100],dep[100],i,k=0,d[100],count=0;
	
	printf("enter no. of trains");
	scanf("%d",&n);
	
	printf("enter arrival times");
	for(i=0;i<n;i++){
	scanf("%d",&arr[i]);	
	}
	
	printf("enter departure times ");
	for(i=0;i<n;i++){
		scanf("%d",&dep[i]);
		if(dep[i]<arr[i]){
			printf("enter departure time greater than its arrival time");
			break;
		}
	}
	
	
	for(i=0;i<n;i++){
		if(i==0){
			d[k]=dep[i];
			count++;
			k++;
		}
		else{
			if(arr[i]<d[smalld(d,k)]){
				d[k]=dep[i];
				count++;
				k++;
			}
			else{
				d[smalld(d,k)]=dep[i];
			}
		}		
	}
	
	printf("min no of platform required are %d",count);
}


int smalld(int d[],int x){
	int i, k,min=d[0];
	if(x==1){
		return 0;
	}
	else{
		for(i=1;i<x;i++){
		if(min>d[i]){
		min=d[i];
		k=i;	
		}
	}
     return k;
	}
	
}

- Shubham Jain July 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

void main(){
	int n,arr[100],dep[100],i,k=0,d[100],count=0;
	
	printf("enter no. of trains");
	scanf("%d",&n);
	
	printf("enter arrival times");
	for(i=0;i<n;i++){
	scanf("%d",&arr[i]);	
	}
	
	printf("enter departure times ");
	for(i=0;i<n;i++){
		scanf("%d",&dep[i]);
		if(dep[i]<arr[i]){
			printf("enter departure time greater than its arrival time");
			break;
		}
	}
	
	
	for(i=0;i<n;i++){
		if(i==0){
			d[k]=dep[i];
			count++;
			k++;
		}
		else{
			if(arr[i]<d[smalld(d,k)]){
				d[k]=dep[i];
				count++;
				k++;
			}
			else{
				d[smalld(d,k)]=dep[i];
			}
		}		
	}
	
	printf("min no of platform required are %d",count);
}


int smalld(int d[],int x){
	int i, k,min=d[0];
	if(x==1){
		return 0;
	}
	else{
		for(i=1;i<x;i++){
		if(min>d[i]){
		min=d[i];
		k=i;	
		}
	}
     return k;
	}
	
}

- shubham jain July 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string reverseEachWordinString(string str)
{
string[] words = str.Split(' ');
StringBuilder strResult = new StringBuilder();
for (int i = words.Length - 1; i >= 0; i--)
{
strResult = strResult.Append(words[i] + " ");
}

return strResult.ToString();
}

- gkr July 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string.h>

int main ()
{
        char str[] = "the sky is blue";
        int i = 0,start,j;
        start = strlen(str)-1;
        while (1)
        {
                for (i=start; i != -1 && (str[i]!=' '); i--);
                for (j=i+1; j<=start; j++)
                        printf("%c",str[j]);
                if (i == -1)
                        break;
                start = i-1;
                printf(" ");
        }


        return 0;
}

- Randhir Kumar July 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string ReverseTheString(string str) {
string[] strArray = str.Split(' ');
string reverseString = string.Empty;
for (int i = strArray.Length - 1; i >=strArray.GetLowerBound(0); i--){
reverseString += strArray[i]+" "; }
return reverseString; }

- Govind August 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string ReverseTheString(string str){
string[] strArray = str.Split(' ');
string reverseString = string.Empty;
for (int i = strArray.Length - 1; i >=strArray.GetLowerBound(0); i--){
reverseString += strArray[i]+" ";}
return reverseString;}

- Govind August 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string ReverseTheString(string str){
            string[] strArray = str.Split(' ');
            string reverseString = string.Empty;
            for (int i = strArray.Length - 1; i >=strArray.GetLowerBound(0); i--){ 
	    reverseString += strArray[i]+" ";}
            return reverseString;}

- Anonymous August 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string ReverseTheString(string str){
            string[] strArray = str.Split(' ');
            string reverseString = string.Empty;
            for (int i = strArray.Length - 1; i >=strArray.GetLowerBound(0); i--){ 
	    reverseString += strArray[i]+" ";}
            return reverseString;}

- govind August 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String text = "this text to be reversed";
String[] list = text.split("\\s");
Collections.reverse(Arrays.asList(list));
String result = String.join(" ", list);
System.out.println(result);

- Molham September 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def reverseWords(sentence):
# Find out the reversed sentence....
reversed_sentence = sentence[::-1]

reversed_word = ' '.join(reversed_sentence)

return reversed_word



sentence = input().strip(' ').split()

print(reverseWords(sentence))

- Shreyas Bhatawdekar September 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter string to reverse:");
		String inputStr = in.nextLine();
		
		String[] sArr = inputStr.split(" ");
		Collections.reverse(Arrays.asList(sArr));
		
		StringBuilder finalStr = new StringBuilder();
		for (String s : sArr) {
			finalStr.append(convertWordUpper(s));
		}
		
		System.out.println("Final output: " + finalStr.toString().trim());
		in.close();
	}

	private static String convertWordUpper(String s) {
		return Character.toUpperCase(s.charAt(0)) + s.substring(1) + " ";
	}

- dattadiptayandev November 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void reverse(String str){
int i=str.length()-1;
String result="";
int word=0;
while(i>=0){
if(str.charAt(i)==' ') {
if(word >0)
result = result.concat(str.substring(i+1,i+1+word));
word=0;
result=result.concat(" ");

}else {
if(i==0){
word++;
result=result.concat(str.substring(i,i+word));
break;
}else {
word++;
}
}
i--;
}

System.out.println(result);

}

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

static void reverse(String str){
        int i=str.length()-1;
        String result="";
        int word=0;
        while(i>=0){
            if(str.charAt(i)==' ') {
                if(word >0)
                    result = result.concat(str.substring(i+1,i+1+word));
                word=0;
                result=result.concat(" ");

            }else {
                if(i==0){
                    word++;
                    result=result.concat(str.substring(i,i+word));
                    break;
                }else {
                    word++;
                }
            }
            i--;
        }

        System.out.println(result);

    }

- ast17 January 02, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void reverseWords(String phrase) {
String[] contents = phrase.split(" ");
int hi = contents.length - 1;
int low = 0;
while(low <= hi) {
String hiWord = contents[hi];
String loWord = contents[low];
contents[hi] = loWord;
contents[low] = hiWord;
low = low + 1;
hi = hi - 1;
}
StringBuilder sb = new StringBuilder();
for(String s : contents) {
sb.append(s + " ");
}
System.out.println(sb.toString() + ".");
}

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

public static void reverseWords(String phrase) { 
		String[] contents = phrase.split(" ");
		int hi = contents.length - 1;
		int low = 0;
		while(low <= hi) {
			String hiWord = contents[hi];
			String loWord = contents[low];
			contents[hi] = loWord;
			contents[low] = hiWord;
			low = low + 1;
			hi = hi - 1;
		}
		StringBuilder sb = new StringBuilder();
		for(String s : contents) {
			sb.append(s + " ");
		}
		System.out.println(sb.toString() + ".");

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

def reverse_string(string):
    words = string.split()
    sentence_reverse = " ".join(reversed(words))
    return sentence_reverse


print(reverse_string("The Sky is Blue"))

- Enthrayu January 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def wordRev(s='The Sky is blue'):
sp = s.split(' ')
print(sp)
nStr = ''
lst = []
for i in range(len(sp)-1,-1, -1):
lst.append(sp[i])
nStr = ' '.join(lst)
return nStr
print(wordRev())

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

def wordRev(s='The Sky is blue'):
    sp = s.split(' ')
    print(sp)
    nStr = ''
    lst = []
    for i in range(len(sp)-1,-1, -1):
        lst.append(sp[i])
    nStr = ' '.join(lst)
    return nStr
print(wordRev())

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

/**
   * Javascript Implementation.
   
   * Reverse the words in string eg - 
   * Input : 'The Sky is Blue '. 
   * output: 'Blue is Sky The'.
*/


function reverseWords(str) {
  // Assumption: There is only one space between words.
  return str.split(' ').reverse().join(' ');
}

const str = 'The Sky is Blue';
console.log("Before Reverse, String is -> " +  str);
console.log("After  Reverse String is -> " +  reverseWords(str));

- Amit Bansal March 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

s1 = "The Sky is Blue"
l1=s1.split()
l2=l1[::-1]
s2=" ".join(l2)

print (s2)

- Imon March 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def revWordsInAString(str):
words = str.split(' ')
newstr = ''
for c in range(len(words)-1,-1,-1):
temp = list(words[c])[::-1]
for i in temp:
newstr+= i
print(newstr)

- SachinK April 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* C solution: 2 pass solution (assuming strlen() is available but can be coded otherwise)*/
void reverse_str(char* src, char * end)
{
char tmp;

if (!src || !end) return;

while (src<end)
{
tmp = *src; *src++ = *end; *end-- = tmp;
}
return;
}

void reverse_words(char *str)
{
int len;
char *t1, *t2, *t3;

if (!str || !(*str)) return;

len = strlen(str);

reverse_str(str, str+len-1);

t1= t2 = str;
while(1)
{
if (*t2 == ' ') // One Space character
{
reverse_str(t1, t2-1);

//Skip all consecutive spaces
while(*t2 == ' ') {
t2++;
if (!(*t2)) return; //End of string
}
t1 = t2;
}
else if (!(*t2)) // Null character, end of string
{
reverse_str(t1, t2-1);
break;
} else t2++;
}
return;
}

void main()
{
char *string = "The Sky is Blue ";
reverse_words(string);
}

- Toseef Koliyariwala May 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in scala we would do something like
val a = "sky is blue".split(" ").reverse.mkString(" ")

- coding_enthusiast September 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A very simple concept is to use a stack and push all the character values into it until we encounter a space between words, if a space is found concatenate all the character values to a string and pop out all the elements from the stack till it is empty, keep doing the same process until all characters in a string is traversed and finally concatenate the remaining character values to the string and return the string.

Implementation:

#include<bits/stdc++.h>
using namespace std;
string reversestring(string str){
stack<char> s;
string st = "";
for(int i = str.length() - 1; i >= 0; i--){
if(str[i] == ' '){
while(!s.empty()){
st += s.top();
s.pop();
}
st += str[i];
}
else
s.push(str[i]);
}
while(!s.empty()){
st += s.top();
s.pop();
}
return st;
}
int main()
{
string str = "The Sky Is Blue";
cout<<reversestring(str)<<endl;

return 0;
}

- swapnilkant11 May 28, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class ReverseWords {
	public static void main(String[] args) {
		System.out.println(reverseWords("The Sky is Blue"));
		System.out.println(reverseWords("My name is Bushan"));
	}
	public static String reverseWords(String string){
		String words[] = string.split(" ");
		String temp = "";
		for(int i = words.length - 1; i >= 0; i--){
			temp += words[i]+" ";
		}
		return temp.trim();
	}
}

- Bushan SC July 24, 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