Adobe Interview Question
Software Engineer / DevelopersInterview Type: Phone Interview
void * createArray(int dimension[], int size)
{
if(size == 1){
void * array = malloc( sizeof(int) *dimension[0]);
return array;
}
void * array = (void *)malloc( sizeof(void *) *dimension[0]);
for(int i = 0; i< dimension[0]; i++){
array[i] = createArray( & dimension[1], size-1);
}
return array;
}
Say we want to print all the numbers with N digits and each digit has a given upperbound, so the number will be A[0]A[1]A[2]...A[N-1] with A[i] <= upperbound[i], then we may do like this:
void printNumber(char num[], int index, int N, int upperbound[])
{
if(index == N){
num[index] = '\0';
puts(num);
return;
}
num[index] = index == 0 ? '1' : '0';
for(; num[index] - '0' <= upperbound[index]; ++num[index]){
printNumber(num, index + 1, N, upperbound);
}
}
If I understand this question correctly, the function is called with N arguments, and the number of arguments is the number of dimensions, and the value of the arguments is the size of each dimension. So ... using C ...
#include <stdarg.h>
int* AllocateArray(int n_args, ...)
{
va_list list;
va_start(list,n_args);
int total = 1;
for (int i = 0; i<=n_args;i++)
total *= va_arg(list,int);
return (int*)malloc(total * sizeof(int));
}
@alex: can u please explain the qquestion?
- preeti January 17, 2014