Barclays Capital Interview Question for Senior Software Development Engineers


Country: United States




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

private static void reverse(char[] c) {
		reverse(c,0,c.length-1);
		
		int wordStart=0;
		for(int i=0;i<c.length;i++)
		{
			if(c[i]==' ')
			{
				reverse(c,wordStart,i-1);
				wordStart=i+1;
			}
			else if(i==c.length-1)
			{
				reverse(c,wordStart,i);
			}
		}
	}

- Vir Pratap Uttam May 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Read the string starting from the end, storing the characters in a variable. If you encounter a "space" character, print the string held by the variable in reverse and clear the variable. Repeat until you've read the entire string. Should run in O(n) time where n is the length of the string.

- chimpinchief May 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is going to be of O(n2) first for loop will be for reading string from end to start
And then second for loop within the first for reversing the word which is been stored before a space is found.

- Shubham Agarwal July 30, 2019 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

Reverse the sentence followed by reversing each word in the sentence.

- Anonymous May 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That takes two passes, so not as efficient as reversing each word as you come to it and then starting again when you get to a space (which is what chimpinchief does).

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

Store the strings one by one in linked list and traverse the list from end node .

- Anonymous May 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I forgot to mention that , I was told not to use any collection framework

- cCAACc May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

better implement stack and add each word in stack at the end pop each word.

- Amit Khatri July 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Reverse{
static String input = "I love you";
static char[] in = input.toCharArray();
public static void main(String[] args){

int end=0;
int start=0;
for(int i=0;i<in.length; ){
while(i<in.length && in[i]!=' ')
i++;

end=i-1;
in= reverse(start, end, in);
start=i+1;
i=start;
}
in = reverse(0, in.length-1,in);

for(int i=0;i<in.length;i++){
System.out.print(in[i]);
}

}
public static char[] reverse(int i, int j, char[] word){
if(i==j)
return word;

while(i<j){
char temp = word[i];
word[i]=word[j];
word[j]=temp;
i++;
j--;
}
return word;

}
}

- India May 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Did you calculate the complexity of you program?? Please think from that point. How about the below code?
import java.util.Arrays;
import java.util.StringTokenizer;




public class Test {
public static void main(String[] args) {
String str = "i love you";
String[] strArray = str.split(" ");
StringBuilder sbuilder = new StringBuilder();
for(int i=strArray.length -1;i>=0; i--){
sbuilder.append(strArray[i]).append(" ");
}
System.out.println(sbuilder.toString());
}

}

I have used only one for loop. complexity O(n).

- undefined May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseTheLine {

public static void main(String[] args) {
String str = "I LOVE YOU";
String [] strArr = str.split(" ");

String reverse="";

for(String temp: strArr){
reverse = temp+" "+reverse;
}
System.out.println(reverse);
}

}

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

public class ReverseTheLine {

public static void main(String[] args) {
String str = "I LOVE YOU";
String [] strArr = str.split(" ");

String reverse="";

for(String temp: strArr){
reverse = temp+" "+reverse;
}
System.out.println(reverse);
}

}

- Sujeet May 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
    char str[]="i love you";
    char words[100][20];
    int i;
    int tok=0;
    char * res;
    res=strtok(str," ");
    while(res!=NULL)
    {
       strcpy(words[tok++],res);
       res=strtok(NULL," ");
    }
    
    for(i=tok-1;i>=0;i--)
    printf("%s ",words[i]);
   
getch();
  return(0);
}

- puneet kumar May 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void OderingOfString(string p)
{
StringBuilder strb = new StringBuilder();
string[] str = p.Split(' ');
var list = str.Reverse();
foreach (string s in list)
{
strb.AppendFormat("{0} ",s);
}
Console.WriteLine(strb.ToString().TrimEnd());
}

- Anonymous May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

protected static string ReverseOrderingOfWords(string s)
        {
            string temp = string.Empty;
            string reverse = string.Empty;
            int counter = 0;

            foreach (char ch in s)
            {
                counter++;

                if (!char.IsWhiteSpace(ch))
                {
                    temp = temp + ch;
                }
                else
                {
                    reverse = " " + temp + reverse;
                    temp = string.Empty;
                }

                if (counter == s.Length)
                {
                    reverse = temp + reverse;
                }
            }

            return reverse;
        }

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

#include <iostream>

using namespace std;

string ReverseWordOrders(string s)
{
    int index = s.length() - 1;
    string temp,result;

    for(int i=0; i<=index; i++)
    {
        if(s[i] != ' ')
        {
            temp = temp + s[i];
        }
        else
        {
            result = ' ' + temp + result;
            temp.clear();
        }

        if(i == index)
        {
            result = temp + result;
        }
    }

    return result;
}

int main()
{
    string myString;

    cout << "Enter any string:";
    getline(cin,myString);

    myString = ReverseWordOrders(myString);

    cout<< "String after reversing word orders:" << myString << endl;

    return 0;
}

- Nish May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is in place reverse. you need store it in a new one.
int reverse(char s[],char *p)
{
char *stack[10];
stack[0] = s;
int i=1;
while(*s!='\0')
{
if(*s==' ')
{
*s='\0';
stack[i++] = s+1;
}
s++;

}

for(int j=i-1;j>=0;j--) cout << stack[j]<< " ";
}

- crickmasterdhawal June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Rev
{
public static void main(String[] args)
{
String s1="India is most famous country in the world";
String str[]=s1.split(" ");
for(int i=s1.length()-1;i>=0;i--)
{
sop("Rev of String is:"+str[i]);
}
}
}

- Anonymous July 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

///public class Split_ILU {

public static void main(String[] args) {
// TODO Auto-generated method stub

String str = "I love you";

String[] str_arr=str.split(" ");

StringBuilder stb=new StringBuilder();
System.out.println("str_arr.length="+str_arr.length);
for(int i=(str_arr.length-1);i>=0;i--){
stb.append(str_arr[i]);
stb.append(" ");
}
System.out.println("stb="+stb.toString());
}

}
///

- RakeshK August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void reverse(String[] args) {
String [] result = new String [args.length];
int counter =0;
for (int i = args.length -1 ; i >=0 ; i-- )
{
result[counter]= args[i].toString();
counter++;

}
//System.out.println( Arrays.toString( args ) );
//System.out.println( Arrays.toString( result ) );
//0(n) one loop

}

- Jeff August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's the code in C
It calculates in O(n^2)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char *str="I am Sam but my friend is also Sam";
    char *str2=NULL,*str1=str,*str3=NULL;
    while(*str1!='\0')
    {
        str1++;
    }
    str1--;
    str2=str1;
    while(*str2!=str[0])
    {
    while(*str2!=' ')
    {
        str2--;
    }
    str3=--str2;
    str2++;
    while(str2!=str1+1)
    {
        printf("%c",*str2);
        str2++;
    }
    str1=str3;
    str2=str3;
    }
if(str[1]==' ')
printf(" %c",str[0]);
else
printf("%c",str[0]);
}

- rathiprashant3 October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my implementation of the program in java. It runs in O(N). Hope it helps.
public static void reverseWords(String str)
{
int end=str.length();
int endIndex;
endIndex=str.length();
StringBuilder sb=new StringBuilder();
while(end!=0)
{
end--;
if(str.charAt(end)!=' ' && end!=0)
continue;
else if(str.charAt(end)==' ')
{
sb.append(str.substring(end+1, endIndex));
sb.append(" ");
endIndex=end;
}
else if(end==0)
sb.append(str.substring(end, endIndex));
}
System.out.println(sb);
}

- Shaarif November 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have got an easy and basic solution for this.

public static void main(String[] args) {
    String s="I Love You and You Love Me";
    s=" "+s;
    String s1="";
    int l=s.length()-1;
    int ei=0;
    while(l>=0){
      if(s.charAt(l)==' '){
        s1=s1+s.substring(l+1, ei+l+1)+" ";
        l--;
        ei=0;
      }
      else{
        l--;
        ei++;
      }
    }
    char c ='A';
   
    System.out.println("Output string is "+s1+" and ascii of A is "+(int)c);
  }

- razik.islam February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't get confused with the char c. it is redundant, just ignore the use of it.

- razik.islam February 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args)
{
String s="I love you";
String[] str = s.split(" ");
StringBuilder ss = new StringBuilder();
for(int i=str.length-1;i>=0;i--)
{
ss.append(str[i]);
ss.append(" ");
}
System.out.println(ss.toString());
}

- mayank March 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringTest {
public static void main(String[] args)
{
String s="i love you";

String []ss=s.split(" ");
for(int i=ss.length-1;i>=0;i--)
{
System.out.print(" ");
System.out.print(ss[i]);}// YOU LOVE I


}
}

- Anurag upadhyay April 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Anurag Upadhyay
Noida,India
any query ---anurag.u007@gmail.com

- Anurag upadhyay April 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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


char *ptr = "I LOVE YOU";
char *ptr2 ,*ptr3; //for reversal//
int temstrlen = 0;
char *ptr3;

void main()
{
int len=strlen(ptr);
int len1 =len;
int i=0;
ptr2 = (char *) malloc(len+1);
ptr3=ptr2;

while(len)
{

if ((*(ptr+len-1)) !=' ')
{ temstrlen ++; len --;

if(len !=0) continue;
}
//found blank;

for(i=0;i<temstrlen;i++)
{ *ptr2 = *(ptr+len+i);
ptr2++;
}

if(len!=0) {*ptr2=' '; ptr2++; temstrlen=0; len--;}
} //end of while

*(ptr3+len1) = '\0';
printf("%s \n",ptr3);

}

- vathsala September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.DataStructure;

public class Reverse {
public static void main(String[] args) {
String str = reverse("IM LOVE MY INDIA");
int i = 0, j = 0;
String str2 = "";
int len = str.length();
while (j < len) {
if (str.charAt(j) == ' ') {
str2 = str2 + reverse(str.substring(i, j)) + " ";
i = j + 1;
j = i;
} else if (j == str.length()-1) {
str2=str2+reverse(str.substring(i, j+1));
i = j + 1;
j = i;
} else {
j++;
}
}
System.out.println(str2);
}
static String reverse(String str) {
String str1 = "";
if (str.length() == 0) {
return str;
} else if (str.length() == 1) {
return str;
} else {
return str1 + str.charAt(str.length() - 1)
+ reverse(str.substring(0, str.length() - 1));
}
}
}

- chandra Mohan September 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.DataStructure;

public class Reverse {
	public static void main(String[] args) {
		String str = reverse("IM LOVE MY INDIA");
		int i = 0, j = 0;
		String str2 = "";
		int len = str.length();
		while (j < len) {
			if (str.charAt(j) == ' ') {
				str2 = str2 + reverse(str.substring(i, j)) + " ";
				i = j + 1;
				j = i;
			} else if (j == str.length()-1) {
					str2=str2+reverse(str.substring(i, j+1));
					i = j + 1;
					j = i;
			} else {
				j++;
			}	
		}
		System.out.println(str2);
	}
	static String reverse(String str) {
		String str1 = "";
		if (str.length() == 0) {
			return str;
		} else if (str.length() == 1) {
			return str;
		} else {
			return str1 + str.charAt(str.length() - 1)
					+ reverse(str.substring(0, str.length() - 1));
		}
	}
}

- chandra Mohan September 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.DataStructure;

public class Reverse {
public static void main(String[] args) {
String str = reverse("IM LOVE MY INDIA");
int i = 0, j = 0;
String str2 = "";
int len = str.length();
while (j < len) {
if (str.charAt(j) == ' ') {
str2 = str2 + reverse(str.substring(i, j)) + " ";
i = j + 1;
j = i;
} else if (j == str.length()-1) {
str2=str2+reverse(str.substring(i, j+1));
i = j + 1;
j = i;
} else {
j++;
}
}
System.out.println(str2);
}
static String reverse(String str) {
String str1 = "";
if (str.length() == 0) {
return str;
} else if (str.length() == 1) {
return str;
} else {
return str1 + str.charAt(str.length() - 1)
+ reverse(str.substring(0, str.length() - 1));
}
}
}

- chandra Mohan September 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Reverse {
	public static void main(String[] args) {
		String str = reverse("IM LOVE MY INDIA");
		int i = 0, j = 0;
		String str2 = "";
		int len = str.length();
		while (j < len) {
			if (str.charAt(j) == ' ') {
				str2 = str2 + reverse(str.substring(i, j)) + " ";
				i = j + 1;
				j = i;
			} else if (j == str.length()-1) {
					str2=str2+reverse(str.substring(i, j+1));
					i = j + 1;
					j = i;
			} else {
				j++;
			}	
		}
		System.out.println(str2);
	}
	static String reverse(String str) {
		String str1 = "";
		if (str.length() == 0) {
			return str;
		} else if (str.length() == 1) {
			return str;
		} else {
			return str1 + str.charAt(str.length() - 1)
					+ reverse(str.substring(0, str.length() - 1));
		}
	}
}

- chandra Mohan September 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{ char *p,*q;
int i,j;
clrscr();
gets(p);
i=strlen(p);
q=(char*)malloc(i*sizeof(char));
q=p;
for(j=i;j>=0;j--)
{ printf("%c",q[j]);
}
}

- Gambhir Rathore August 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{ char *p,*q;
int i,j;
clrscr();
gets(p);
i=strlen(p);
q=(char*)malloc(i*sizeof(char));
q=p;
for(j=i;j>=0;j--)
{ printf("%c",q[j]);
}
}

- Gambhir Rathore August 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{ char *p,*q;
int i,j;
clrscr();
gets(p);
i=strlen(p);
q=(char*)malloc(i*sizeof(char));
q=p;
for(j=i;j>=0;j--)
{ printf("%c",q[j]);
}
}
}

- Anonymous August 29, 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