Yatra.com Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Following solution uses a stack ( i.e, some extra space ) .
1. traverse the string , when ever a space is found we can assume we have reached the end of a word . Now , treat this word as a separate string and push it in a stack . Keep on doing so until the string is finished

2. Now pop the stack & put it back into the original string from the start . While doing so , remember to put a space after every single pop , except the last one .

- saikat January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

More complicated solutions is required if words are separated more than one space.

- AndrewJD January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why a more complicated one is required ? if more than 1 white space is a valid condition between words . Then just eat up the first white space after every word & Push other white spaces in to the stack . while recreating the string , just follow the above rule , with only one modification that don't insert a white space after a white space is poped from the stack .

- saikat January 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

string = "reverse string by words"
    words = string.split()
    words.reverse()
    print " ".join(words)

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

import java.io.*;
import java.util.*;
public class str1
{
public static void main(String a[])
{
String str="my name is sonam";
String[] s=new String[100];
int i;
StringTokenizer st=new StringTokenizer(str);
int n=st.countTokens();
//st.nextToken();
for(i=0;i<n;i++)
s[i]=st.nextToken();

for(i=n-1;i>=0;i--)
System.out.print(" "+s[i]);
}
}

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

public static void main(String[] args) {
String str="this is yatra";
String rev = null;
for(int i=(str.length()-1);i>=0;i--){
char ch=str.charAt(i);
rev=String.valueOf(ch);
System.out.print(rev);
}

o/p:artay si siht

- naveen yadav July 25, 2014 | 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