NVIDIA Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

#include<stdlib.h>
#include<stdio.h>

void task1(){
    printf("I am base class object\n");
}

void task2(){
    printf("I am derived class object\n");
}


typedef void (*fnptr_t)(void );

static fnptr_t vatable_A[1]={task1};
static fnptr_t vatable_B[1]={task2};

struct A{
    fnptr_t *vptr;
    int _a;
};

struct B{
    fnptr_t * vptr;
    int _b;
};

void init_A(struct A* p ){
    p->vptr= vatable_A;
    p->_a=0;
}

void init_B(struct B* p){
    p->vptr= vatable_B;
    p->_b=0;
}

void func(void* ob){
    /*And this something dark magic.*/
    struct A* p = (struct A*)ob;
    p->vptr[0]();

}

int main(){
    struct A ob_a;
    struct B ob_b;
    init_A(&ob_a);
    func((void*)&ob_a);

    init_B(&ob_b);
    func((void*)&ob_b);
    return 0;
}

- Manish January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, I will be very wrong if I say C++ implements polymorphism like above code snippet. But the above code shows a small glimpse about the trick behind virtual function. There are a lot of things which "C++" compiler does behind the scene.

- Manish January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code seems like overloading. A function name with two different arguments.
Polymorphism is a concept to describe a function of class which behave different when referred by different subclass object.

- WuPengfei.WPF April 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

function overloading "is" a type of polymorphism (ad hoc polymorphism to be precise).

- notthewronganswer May 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question asks about runtime polymorphism which is more like method overriding than overloading isn't it??

- confused April 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct {
	//some field
	int a;
}StructTwo;


typedef struct {
	//some field
}StructOne;


typedef struct {
	int type;
	union {
		StructOne one;
		StructTwo two;
	};

}BaseStruct;



void foo(BaseStruct b){
	switch (b.type) {
		case 1:
			//do something on b.one
			break;
		case 2:
			//do something else b.two
			break;
	}
}


int main(){
	BaseStruct base;
	StructOne one;
	StructTwo two;
	base.type=1;
	base.one=one;
	foo(base);
	base.type=2;
	base.two=two;
	foo(base);
	return 0;

}

- knotman September 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>                                                              
#include <stdlib.h>                                                             
                                                                                
typedef void (*fnptr_t)(void*);                                                 
                                                                                
struct A {                                                                      
    const fnptr_t* vtable;                                                      
    int field_A;                                                                
};                                                                              
                                                                                
void A_vfunc_0(void* obj)                                                       
{                                                                               
    struct A* this = (struct A*)obj;                                            
    printf("%s field_A = %d\n", __FUNCTION__, this->field_A);                   
}                                                                               
                                                                                
// B inherent from A                                                            
struct B {                                                                      
    struct A parent;                                                            
    int field_B;                                                                
};                                                                              
                                                                                
void B_vfunc_0(void* obj)                                                       
{                                                                               
    struct B* this = (struct B*)obj;                                            
                                                                                
    A_vfunc_0(this);                                                            
    printf("%s field_B = %d\n", __FUNCTION__, this->field_B);                   
}                                                                               
                                                                                
// vtables for A & B                                                            
const fnptr_t vtable_A[] = { A_vfunc_0 };                                       
const fnptr_t vtable_B[] = { B_vfunc_0 };                                       
                                                                                
                                                                                
// constructors for A & B                                                       
void initA(struct A* obj, int v)                                                
{                                                                               
    obj->field_A = v;                                                           
    obj->vtable = vtable_A;                                                     
}                                                                               
                                                                                
struct A* createA(int v)                                                        
{                                                                               
    struct A* r = (struct A*) malloc(sizeof(struct A));                         
    initA(r, v);                                                                
    return r;                                                                   
}                                                                               
                                                                                
void initB(struct B* obj, int v0, int v1)                                       
{                                                                               
    initA(&(obj->parent), v0);                                                  
    obj->field_B = v1;                                                          
    obj->parent.vtable = vtable_B;                                              
}                                                                               
                                                                                
struct B* createB(int v0, int v1)                                               
{                                                                               
    struct B* r = (struct B*) malloc(sizeof(struct B));                         
                                                                                
    initB(r, v0, v1);                                                           
    return r;                                                                   
}                                                                               
                                                                                
// call virtual functions                                                       
void invoke_vfunc(struct A* obj, int idx)                                       
{                                                                               
    obj->vtable[idx](obj);                                                      
}                                                                               
                                                                                
int main(int argc, char** argv)                                                 
{                                                                               
    struct A* objA = createA(1);                                                
    struct A* objB = (struct A*)createB(2, 3);                                  
                                                                                
    printf("call A func:\n");                                                   
    invoke_vfunc(objA, 0);                                                      
                                                                                
    printf("call B func:\n");                                                   
    invoke_vfunc(objB, 0);                                                      
                                                                                
    free(objA);                                                                 
    free(objB);                                                                 
                                                                                
    return 0;                                                                   
}

- peng.guo.cn July 23, 2018 | 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