Expedia Interview Question
Software DevelopersCountry: United States
Interview Type: In-Person
#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);
}
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
*/
#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);
}
#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;
}
}
#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;
}
}
#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;
}
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) + " ";
}
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);
}
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);
}
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() + ".");
}
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() + ".");
/**
* 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));
/* 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);
}
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;
}
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();
}
}
- NoOne July 24, 2017