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

- ker July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

More specifically, something like:

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

- eugene.yarovoi July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Naveen July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Code above is working fine.

- eugene.yarovoi July 15, 2012 | Flag
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); 
}

- Noobie programmer trying to be cool July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I mean..

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

- Anonymous July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Nachiketa July 10, 2012 | Flag
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

- Deven Bhooshan July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with eugene

- Amit November 29, 2012 | Flag
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); 
}

- ashot madatyan July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Aashish July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- ashot madatyan July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- eugene.yarovoi July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- sun.wael July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- ashot madatyan July 09, 2012 | Flag
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;
}

- Aashish July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- ciara July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Whiner July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- nerd July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

codes-to-problem.blogspot.in/

- niraj.nijju July 07, 2012 | Flag Reply
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;
}

- Avi July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I spy a loop...

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Avi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Avi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi July 08, 2012 | Flag
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.

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

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

- Deepshikha July 12, 2012 | Flag Reply
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);



}

- aaman.singh85 October 19, 2012 | Flag Reply
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??

- Anonymous July 24, 2016 | Flag Reply
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

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

Hi anonymous,

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

Thanks,
SR

- SR July 08, 2012 | 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