Interview Question


Country: India




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

I dont think it is possible........

rather only the length common in both can be swapped

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

yeah right...tried tomorrow a lot to solve this kind of question.

- v009 August 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That string is not read only because it doesn't point to hard coded string.It contains those characters only.

- Ajax September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the two strings aaa and bbb are stored on stack.They can be swapped character by character.But they point to the same string
if it was char* aaa = "hi" and char* bbb = "world". Then it should be in read-only data segment, we cant swap the characters even.

- nk September 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

when you create an array of any type, the compiler creates a const pointer for the base address of that array. and since it is a const pointer you cannot modify the size of the array. And since you created the object as an array and not using a pointer to dynamically allocate the memory you cannot call delete [] on the array, therefore there is no way to change the size of the array.

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

Rather common Java question, since Java abstracts away realloc...

char[] swap(char a,char b)
{
char diff = a ^ b;
a=diff^arr2;
b=diff^arr1;
return new char[]{a,b};
}

void append(List<char> src,int start,List<char> dest)
{
dest.addAll(start,src);
}

void swap(List<char> arr1,List<char> arr2)
{
if(arr1.length==0 && arr2.length==0)return;

int i=0;

for(;i<arr1.length && arr1.length==arr2.length;i++)
{
char[] swapped = swap(arr1.get(i),arr2.get(i));
arr1.set(i,swapped[1]);
arr2.set(i,swapped[0]);
}



if(arr1.length > arr2.length)
{
append(arr1,i,arr2);
}

else if(arr1.length < arr2.length)
{
append(arr2,i,arr1);
}
}

- Jack August 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

^ you don't declare an array like that in Java...so I'll assume this is a C++/ C problem.

- Malluce August 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C We can not reallocate char aaa[] & bbb[];

we Can reallocate only char pointer

- Abhijeet Rokade August 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is not possible to swap. You will get run time failure like stack is corrupted.

- suhaskulkarni100 September 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will work, but we need to null terminate the strings properly after swap.

I tried in VS compiler.. It works fine.

- Anonymous August 22, 2013 | 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