VMWare Inc Interview Question for Software Engineer / Developers






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);
			}
		}
	}

	private static void reverse(char[] c, int i, int j) {
		if(i>=j)
			return;
		for(int k=i;k<=(i+j)/2;k++)
		{
			char temp=c[k];
			c[k]=c[i+j-k];
			c[i+j-k]=temp;
		}
	}

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

void wordrev(char * str) {
  char * last = str;
  char * ptr = str;
  strrev( str );
  while( *ptr != 0 ) {
     if ( *ptr == ' ' ) {
        *ptr = '\0';
        strrev( last );
        *ptr = ' ';
        last = ptr + 1;
     }
     ptr++;
  }
  strrev(last);  
  return;
}

void strrev(char * str) {
  int len = strlen(str);
  int len_2 = len / 2;
  char temp;
  int i;
  for(i = 0; i < len_2;i++) {
     temp = str[i];
     str[i] = str[len-i-1];
     str[len-i-1] = temp;    
  }
  return;
}

- Zaphod January 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice one

- Div March 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <stdio.h>

char sentence[] = "This is my big beautiful sentence having multiple words";

void reverse(char *s)
{
    char *tmp = s;

    while(s && *s && *s != ' ')
	s++;
    if(*s == ' ') {
	*s = '\0';
	reverse(++s);
    }
    printf("%s ",tmp);
}


int main(void)
{
    reverse(sentence);
    printf("\n");

    return 0;
}

- Prem Mallappa August 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

really awesome !!

- laterGator October 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the spaces are lost !!!

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

In C# its easy
Take the sentence into string.Then store each word of the string in string array by performing string.split(' ') then apply a decrement loop on this string array and append each word using string builder.

- MIT January 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in C u can use strtok to do the tokenizing part
and use strcat to do the builder part

- Anonymous January 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in java, you can use the StringTokenizer class

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

1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 int
  6 main ()
  7 {
  8     char str[] = "This is a nice world";
  9     int len = strlen ( str );
 10     int pos = len;
 11     char *rev = (char *)malloc( len + 1 );
 12     memset ( rev, ' ', len );
 13     rev [len] = '\0';
 14     printf ("rev = X%sX\n", rev );
 15     char *str1 = strtok ( str, " " );
 16     len = strlen ( str1 );
 17     strncpy ( rev + pos - len, str1, len );
 18     pos = pos - len - 1;
 19     while ( (str1 = strtok ( NULL, " " )) != NULL )
 20     {
 21         len = strlen ( str1 );
 22         strncpy ( rev + pos - strlen ( str1 ), str1, strlen (str1));
 23         pos = pos - strlen ( str1 ) - 1;
 24     }
 25 
 26     printf ("rev = %s\n", rev );
 27     free ( rev );
 28     return 0;
 29 }

- shankha April 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
String s = new String("this is a new world");
String c[] = s.split(" ");
for (int i = c.length -1; i >=0 ; i--){
System.out.print(c[i] + " ");
}
}

- Venkatesh May 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you are not allowed to use any library functions to split the string, nor to create any additional strings and/or arrays, in Java you can do it in O(n) by reversing the whole string first and then reversing the words. Here is a solution:



public class WordReversing {
public static char[] reverse(char[] string) {
reverseChars(string, 0, string.length - 1); // reverse whole string
int wordStart = 0;
int wordEnd;

while (wordStart < string.length) {
// skip multiple spaces and find the next word start position
while (wordStart < string.length && string[wordStart] == ' ') {
wordStart++;
}

wordEnd = wordStart;
while (wordEnd < string.length && string[wordEnd] != ' ') {
wordEnd++;
}
reverseChars(string, wordStart, wordEnd - 1); // reverse the word
wordStart = wordEnd + 1;
}

return string;
}

public static void reverseChars(char[] string, int from, int to) {
if (from < 0 || to >= string.length || from >= to) {
return;
}
while (from < to) {
char temp = string[from];
string[from] = string[to];
string[to] = temp;
from++;
to--;
}
}

public static void main(String[] args) {
char[] s = " This is a test. ".toCharArray();
System.out.println(reverse(s));
}
}

- CyberGene November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey75291" class="run-this">public class WordReversing {
public static char[] reverse(char[] string) {
reverseChars(string, 0, string.length - 1); // reverse whole string
int wordStart = 0;
int wordEnd;

while (wordStart < string.length) {
// skip multiple spaces and find the next word start position
while (wordStart < string.length && string[wordStart] == ' ') {
wordStart++;
}

wordEnd = wordStart;
while (wordEnd < string.length && string[wordEnd] != ' ') {
wordEnd++;
}
reverseChars(string, wordStart, wordEnd - 1); // reverse the word
wordStart = wordEnd + 1;
}

return string;
}

public static void reverseChars(char[] string, int from, int to) {
if (from < 0 || to >= string.length || from >= to) {
return;
}
while (from < to) {
char temp = string[from];
string[from] = string[to];
string[to] = temp;
from++;
to--;
}
}

public static void main(String[] args) {
char[] s = " This is a test. ".toCharArray();
System.out.println(reverse(s));
}
}
</pre><pre title="CodeMonkey75291" input="yes">
</pre>

- Anonymous November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good one!!!

- sameer December 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdio.h>

using namespace std;

void reverseString(char* input, int start, int end)
{
if (end - start == 0)
{
return;
}
else if (end - start == 1)
{
char temp = input[start];
char temp2 = input[end];

input[start] = temp2;
input[end] = temp;
return;
}

for(int i = start ; i <= start + (end-start)/2 ; i++)
{
//printf("\r\n swapping %d and %d",i , (start + end - i));
char temp = input[i];
char temp2 = input[start + end - i];

input[i] = temp2;
input[start + end - i] = temp;
}
}

void reverseWords(char* input, int length)
{
int start = 0;
for(int i = 0 ; i <= length ; i++)
{
if (input[i] == ' ' || input[i] == '\0')
{
reverseString(input, start, i-1);
start = i + 1;
}
}

reverseString(input,0, length-1);
}

- Nitin Bhatt December 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C code:

int main()
{
	char buffer[] = "this is the best problem";
	int i = strlen(buffer);
	int j = i;

	while(j > 0) {
		char ch = buffer[j];
		if(ch == ' ') {
			i = j + 1;
			printf("%s \n", buffer + i);
			buffer[i] = 0;
		}
		--j;
	}

	printf("%s \n", buffer);
	return 0;
}

- hcmurthy February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String reverse(String myString)
{
    StringBuilder sb = new StringBuilder(myString);
    String rev=sb.toString();
    StringTokenizer st = new StringTokenizer(rev);
    sb.delete(0,sb.length());
    while(st.hasMoreTokens())
    {
    sb.append(new StringBuilder(st.hasMoreTokens).reverse());
    sb.append(" ");
    }
       return sb.toString();
}

- dushy February 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String[] words = sentence.split(" ");
Collections.reverse(Arrays.asList(words));
                
StringBuffer sbuffer = new StringBuffer();
for (String str : words) {
    sbuffer.append(str + " ");
}

return sbuffer.toString();

- Nikhil Patwardhan May 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In javascript you can do it in 1 line,

var original = 'Hello World';
var reversed = original.split(' ').reverse().join(' ');

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

private static String swap(String stringToSwap) {
String[] arr = testStr.split(" ");
int endIndex = arr.length - 1;
int j = endIndex;
String temp = "";
for (int i = 0; i < endIndex / 2; i ++){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j --;
}
StringBuilder sb = new StringBuilder();
for(int k = 0; k < arr.length; k ++){
sb.append(arr[k]);
sb.append(" ");
}
return sb.toString();
}

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

#using PERL

$input="Wednesday is great day";
@inpuarr=split(" ",$input);
@newoutarr=reverse(@inpuarr);
for ($i=0;$i<scalar(@newoutarr);$i++)
{ print "$newoutarr[$i]","\n";}

- Abhishek Shah January 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

reverse_string(char  *str)
{
  char  ch, *str1 = str+((strlen(str))-1);

  if(!str)
    return;

  while(str<str1)
  {
    ch = *str;
    *str = *str1;
    *str1 = ch;
    str++; str1--;    
  }

  return;
}

- mbm June 23, 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