VMWare Inc Interview Question for Member Technical Staffs


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 3 vote

Solution based on swap function

void reverse(std::string& s){
	int i, j;
	for (i=0, j = s.length() - 1; i < j ; ++i, --j)
		swap(s[i], s[j]);
}

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

Using recursion as:

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

void stringRev(char str[],int i,int n)
{
	static int j;
	if(i<n)
	{
		char ch=str[i];
		stringRev(str,i+1,n);
		str[j]=ch;
		printf("%c",str[j]);
		j=j+1;
	}
	str[j]='\0';
}
int main()
{
	char str[]="asd cdfv gfd";
	int n=sizeof(str)/sizeof(str[0]);
	stringRev(str,0,n);
}

- vgeek July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the complexity of the method is O(n). correct me if I m wrong.

- yogi.rulzz July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In place basically means that you dont have to use any extra memory like you should make the changes using only local variable. The complexity would be O(n) because you have to call reverse for every character in the string. Same is the case while swapping the characters of the integers.

- vgeek July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@vgeek:

In-place means that you should update the original string rather than creating a new one.
In your recursive call every time creating new string "char str[] "

Its simple just swap begin with end chars

void reverse_in_place(char a[],int n) {
    for(i=0; i< n/2 ; i ++)
     swap(a[i], a[n-i]); 
}

- pirate July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I know that this one could be done by swapping too. That is why i mentioned earlier in my comment. But i am just using a single local variable to reverse the changes. I am not creating the entire string. Its not char str[]. Its char c. And further in swap also you use a local variable like temp to swap the characters. I am using local variable here to store the previous character. Its the same as every time when you call swap function then also a local variable is created to do the required operation.

- vgeek July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@vgeek:
1) recursive call takes huge stack memory
2) your recursive function stringRev(char str[],int i,int n)
which every time creates new string and str points to the beginning of string

- pirate July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Venkatesh
str points to the character in the string. I know that there will be recursion stack space. But at every step one condition is being checked and that condition is put to the stack. So the condition is checked n-1 times so n-1 different allocation frames will be there in the stack. Thats I think the same as that of the for loop as there also the code is moving in the loop n times. Correct me if i am wrong

- vgeek July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Swapping a string inplace
   public static String reverse(String input)
   {
 char[] inputChars = input.toCharArray();
 int len = input.length();
 for(int i = 0; i < len/2 ; i++)
 {
     inputChars[i] = (char)((int)inputChars[i] ^ 
                                         (int)inputChars[len - i - 1]);
     inputChars[len - i - 1] = (char)((int)inputChars[i] ^ 
                                         (int)inputChars[len - i - 1]);
     inputChars[i] = (char)((int)inputChars[i] ^ 
                                          (int)inputChars[len - i - 1]);
 }
 return new String(inputChars);
  }

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

swap the first character of the string with the last character of the string and do this in a loop till the middle of the string!

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

void reverse_string()
{
char* p=NULL;
char*s=NULL;
char a[100]={0};
int len=0;
int i=0;
char temp=0;
p=a;
s=a;
printf("enter the details");
fflush(stdin);
gets(p);

len=strlen(s);

while(i<=len/2)
{
temp=s[i];
s[i]=s[len-1];
s[len-1]=temp;
i++;
len--;
}

puts(s);


}

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

string reverse(string title)
{
	for(int k=0, j=title.length()-1; k<j; ++k, --j)
	{
		char temp_letter = title[k];
		title[k] = title[j];
		title[j] = temp_letter;
	}
	return title;
}

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

for(int i=0, j=strlen(str)-1; i<strlen(str)/2;i++,j--)
{
string s;
s = str.charAt[i];
str.charAt[i] = str.charAt[j];
str.charAt[j] = s;
}

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

public static String reverseStr(String str, int len)
{ char c[] = str.toCharArray();
for(int i=0;i<len/2;i++)
{
char temp = c[i];
c[i]=c[len-1-i];
c[len-1-i]=temp;
}
str= str.copyValueOf(c);
return str;
}

- Vinod Madyalkar September 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Just swap begin with end chars

void reverse_in_place(char a[],int n) {
    for(i=0; i< n/2 ; i ++)
      swap(a[i], a[n-i]); 
}

- pirate July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will generate a runtime error - array out of bounds on first iteration (n - i, when i = 0)

- dial.alpha July 28, 2013 | Flag


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