Cubic Transportation Systems Limited Interview Question
Technical Support EngineersCountry: United States
Interview Type: In-Person
More specifically, something like:
void copy (char * dest, char* src)
{
*dest = *src;
if (*src != '\0')
{
copy (dest + 1, src + 1);
}
}
I would note, of course, that there are serious limitations to this. Many implementations of C don't support a huge amount of call depth in the stack, so you might have a stack overflow for large strings. Recursion also uses a whole bunch of extra space.
@euqene.yarovoi
What ever you proposed is correct. But i have some code corrections here.
void copy (char * dest, char* src)
{
if (*src == '\0') { *dest = '\0';
return; }
*dest = *src;
copy (dest + 1, src + 1);
}
class dev{
public static void main(String args[]){
int a[]={1,2,3,4};
int b[]={1,3,4};
a=b;
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
}
}
Output-
1
3
4
I like the out-of-the-box thinking, but it doesn't meet the specifications: copy the *contents* of an array A to *contents* of Array B. That seems to me to imply that the two arrays will have independent contents afterwards and editing one array will not affect the other.
//Assuming array B has enough space for array A
// and the arrays are not overlapping (for overlapping arrays, use memmove)
template <typename T>
void array_cpy(T A[], T B[])
{
size_t sizeA = sizeof(A);
memcpy((void*)&B[0], (void*)&A[0], sizeA);
}
@ashot madatyan, please explain whats going on here:
memcpy((void*)&B[0], (void*)&A[0], sizeA);
AFAIK, memcpy is used to copy one char string to another.
Are you copying byte by byte? why you are typecasting A[0] address to void* ?
memcpy copies symbols from source to destination nad its parameters are void*. And, please note that it makes no difference whether the data is string or not, it treats them as simply binary data.
I would consider memcpy to be a "standard string copy function", even if it's not always used to copy strings.
MemCpy has nothing to do with strings , the only problem is see is sizeof(A) will give you the size of the char type not the size of the array so you have to get the total sum of the array and multiply it by sizeof(A) and do the memcpy using those numbers
True, memcpy has nothing to do with strings. What I meant is that memcpy is still a standard function that is sometimes used for copying strings, so I would interpret this question as saying that this is not allowed. Memcpy is a fine solution if it's allowed.
Just seems too easy if you allow something like that, ya know?
One of the surprising method i would like to share.
However, a prior information must be known which is size of the array to be copied.
Template metaprogramming: The templates must have constant expressions.
Here is the code:
ideone.com/p1cXQ
#include <iostream>
using namespace std;
int size;
template<int n>
void mycopy(int *a,int *b)
{
b[n]=a[n];
mycopy<n+1>(a,b);
}
template<>
void mycopy<3>(int *a,int *b)
{
b[size-1]=a[size-1];
}
int main()
{
int a[4]={1,2,3,4},b[4],i;
size=sizeof(a)/sizeof(a[0]);
mycopy<0>(a,b);
for(i=0;i<4;i++)
printf("%d ",b[i]);
return 0;
}
For those not familiar with this technique: this is a technique specific to C++. Basically, the code for the copying will be produced at compile time and be akin to having written dest[0] = src[0]; dest[1] = src[1]; ... dest[n] = src[n]; except that the compiler will automatically generate the code for you. This will 1) make the code size large if the string to be copied is large and 2) require you to know, like shondik said, the size of the string to be copied at compile time. As such it's not a very general solution.
Well that one isn't a big deal. Can easily be repaired by writing four printf statements.
I dont understand how is this solution far from a recursive solution? To me its just a "fancy recursive solution", but a different solution nonetheless. Thanks.
Yes, Its a compile time recursion but many compilers have a compile time recursion have stack depth of 500, gcc has that.
Any string more than 500 would be a problem, Moreover this could be written to do copy without specializing the template for size 3, it could be more generic but again the compile time stack depth is limited. though we can set it to any X amount but that again is not generic.
Could we do this with GOTO or Setjump longjump in C. Otherwise i have no idea how can you loop with out recursion, either it be compile time or runtime.
#include<stdio.h>
void copy_array(int q[], int r[] ,int i)
{
if(i >= 0)
{
r[i]=q[i];
i--;
copy_array(q,r,i);
}
}
int main()
{
int index,length;
int a[]={1,2,3,4,5,6,7,8,9};
int b[15];
length= ( sizeof(a)/ sizeof(a[1]));
copy_array(a,b,length);
for(index=0;index<9;index++)
printf("%d\n",b[index]);
return 0;
}
@eugene.yarovoi
You are an a*****e spy, that loop i put to check after the array is copied don't try to be over smart.
Uh...
void copy_array(int q[], int r[] ,int i)
{
while(i >= 0)
{
r[i]=q[i];
i--;
copy_array(q,r,i);
}
While statements are also considered loops.
Your logic's also incorrect, now that I take a closer look. I think you meant "if" instead of "while". So if you had the "if", then your solution wouldn't have loops.
Possible Solution in Java , comments please
int [] arr = { 1, 2, 3 , 4 ,5 ,6,7};
int [] arrCopy = new int[7];
System.out.print(Arrays.toString(arr)); // This is just to print the array w/o using for loop
System.arraycopy(arr, 0, arrCopy, 0, arr.length);
System.out.print(Arrays.toString(arrCopy)); // This is just to print the array w/o using for loop
use recursion and get it done..!!
- ker July 07, 2012