## Cubic Transportation Systems Limited Interview Question for Technical Support Engineers

Country: United States
Interview Type: In-Person

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

use recursion and get it done..!!

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

More specifically, something like:

``````void copy (char * dest, char* src)
{
*dest = *src;
if (*src != '\0')
{
copy (dest + 1, src + 1);
}

}``````

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

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.

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

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

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

Code above is working fine.

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

Recursion?

``````copy(a,b, n);
{
if(n >  0) {
b[n] = a[n];
copy(a,b,n-1);
}``````

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

I mean..

``````copy(a,b, n)
{
if(n >=  0) {
b[n] = a[n];
copy(a,b,n-1);
}``````

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

sprintf may not work. the arrays need not necessarily be a character array .
However , if it is char array , it should work fine.

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

``````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

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

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.

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

I agree with eugene

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

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

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

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* ?

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

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.

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

I would consider memcpy to be a "standard string copy function", even if it's not always used to copy strings.

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

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

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

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?

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

@sun.wael
The "sizeof" operator returns the number of bytes not the number of elements.

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

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

Comment hidden because of low score. Click to expand.
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.

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

The question says without using loops and you have a for loop in your code

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

Well that one isn't a big deal. Can easily be repaired by writing four printf statements.

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

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.

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

It's recursion, but compile-time recursion. Read my first comment on this post.

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

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.

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

``codes-to-problem.blogspot.in/``

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

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

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

I spy a loop...

Comment hidden because of low score. Click to expand.
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.

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

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.

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

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.

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

Thats what I meant to write was a little drunk sorry :p

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

It's all good. copy_array(a,b,length); should be copy_array(a,b,length-1); to avoid going out of bounds. With that change and changing the "while" to "if", this solution works fine and is equivalent to a bunch of other solutions on this page.

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

It can be done in a single line, just use sprintf() function.

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

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

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

here is the simple tail recursive version.
char* strcp(char*a,char*b,int i=0)
{
b[i]=a[i];
if(a[i]=='\0')
return b;
strcp(a,b,++i);

}

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

In Python
a = [1,2,34,5,6,]
b=[3,4,5,67,]
a.extend[b]
This should include??

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

bitwise xor... keep all zeros at destination and xor with source
intially contents for dest will be zero after xor contents will be the same as source

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

Hi anonymous,

How do you propose we do that for a big chunk of data?

Thanks,
SR

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.

### 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.