Interview Question






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

As we all know Strings is Java are immutable. I would assume the input is in char[] and then use a C-like algorithm to do the reversal in constant space.

- eaparnell December 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String reverse_string(String s) {
String ret = "";
String[] t = s.split(" ");
for(int i = t.length; i > 0; i--) {
System.out.print(t[i - 1] + " " );
}
return ret;
}

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

1) str = reverse string. ( str can be char array or string buffer) incase of string buffer one can use reverse() function.
2) keep a track of start char index (start) and search for space(end index). If space found reverse the substring from start to end. Continue till the END of the string.

constant space here would be start char index and end character index.

any comments?

- master December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

StringTokenizer st = new StringTokenizer(a," ");
		ArrayList a1 = new ArrayList();
		while(st.hasMoreTokens())
		{
			a1.add(st.nextToken());
		}
	Object[] dd=a1.toArray();
	int ss=dd.length;
	for(int i=ss-1;i>=0;i--)
	{
		System.out.println(dd[i] + " ");
	}
		
	}

- vivekarora86 January 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

reverse string and then reverse each word

- tetura February 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String s = "here i am";
String space = " ";
String[] arr = s.split(space);
StringBuffer sb = new StringBuffer();
boolean first = true;
for(int i=arr.length;i>0;i--)
{
if(first)
first = false;
else
sb.append(space);
sb.append(arr[i-1]);
}
System.out.println(sb.toString());

- d February 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is working java code based on "reverse string and then reverse each word". It runs in O(1) space and O(n) time complexity.

public static String reverseWords(String str) {
char[] array = str.toCharArray();
reverseChar(array, 0, array.length);
int start = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == ' ') {
reverseChar(array, start, i);
start = i + 1;
}
}
//Now reverser the final word
reverseChar(array, start, array.length);
return new String(array);
}

private static void reverseChar(char[] array, int startPos, int endPos) {
int size = endPos - startPos;
if (size <= 0) {
throw new IllegalArgumentException("Size can not be " + size);
}
int mid = startPos + (size >> 1);
int lastIndex = endPos - 1;
for (int i = startPos; i < mid; i++) {
char temp = array[i];
array[i] = array[lastIndex];
array[lastIndex--] = temp;
}
}

- brijpal.ism March 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

split on space and put it in a list and print the list in reverse order

- Anonymous July 12, 2010 | 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