CODEMASTERS YOSEMITE PROGRAMMING TEST
C/C++
1)
Write a class that implements a simple list. It must provide methods to add and remove values, and provide some way to iterate through all elements in the list.
2)
Write destructors for these two classes.
class A
{
public:
int *a;
A() { a = new int[10]; }
};
class B : public A
{
public:
int *b;
B() { b = new int[20]; }
};
3)
Given the following definition, write a constructor that sets the value of m_constant.
class CConstant {
public:
const int m_constant;
const int Get() {return m_constant; }
};
4)
Write a copy constructor for the Circle class defined below. In what two situations does the C++ compiler use the copy constructor implicitly?
class Circle {
public:
Circle();
int center_x;
inst center_y;
double r;
};
5)
What is a hash table? What is its purpose, and how does it function?
6)
What does mysteryFunction return?
int mysteryFunction ( int n)
{
int k = 0;
while( n ) {
k++; n = n / 10;
}
return k;
}
Assembly
1)
What does MysteryFunctionAsm() do. What assumptions does it make?
void MysteryFunctionAsm(unsigned char far *addr,
unsigned value1,
int value2)
{
_asm {
les di,addr
mov al,BYTE PTR value2
mov ah,al
mov cx,value1
shr cx,1
rep stosw
}
}
2)
Write a function in assembly language (80x86, 680x0, or PowerPC) that returns the address of the first byte in a memory block that has the most significant bit set. The function will take a pointer to the beginning of a block of RAM, and the size of the memory block. The function must return the address of the first byte, or null if a non-zero byte is not found. The function should be as fast as possible, especially for long searches.
The prototype for the function is:
void* FindNegByte(void *ram, char size, char key_value);
3D
1) Matrix Math
Calculate the sum of these two matrices.
|2 3| |3 -1|
|1 4| + |5 6|
2)
Calculate the product of these two matrices.
|2 3| |5 6|
|1 2| * |3 0|
3)
What does mysteryFunction3D() compute?
struct vector_3d_typ {
// a 3-D vector along with normalization factor
float x,y,z,w;
} vector_3d,*vector_3d_ptr;
float mysteryFunction3D_1(vector_3d_ptr u,vector_3d_ptr v)
{
return( (u->x * v->x) + (u->y * v->y) + (u->z * v->z));
}
4)
What is the identity matrix of a 3 x 3 matrix?
5)
Write a function to multiply two 16.16 fixed point numbers. Where...
typedef long FIXED;
6)
What is a BSP tree? How does it function?
Internet
1)
Write a function to load into memory the html page at http://www.yahoo.com.
2)
Re-write the simple list class for the first class so that it is threadsafe.
Do anyone like to try this?
Assembly 2 -
org 100h ; entry point for .com
jmp findaddress
addrss db 00h,00h,00h,00h ; 4 bytes for address
string db 128 dup (?) ; same value but in string form
buffer db 0FFFFh dup (?) ; the buffer we will search
; heres where the magic happens #########################################
pusha
push es
findaddress:
lea di,buffer ; buffer we will be searching
xor cx,cx ; cx will be decrimented at loop for maximum search size
searchit:
lodsb ; get byte and increase di (pointer)
and al,80h ; set all but the high order bit to zero
or al,al ; quick compare
jnz foundit ; if al isn't zero than we have our buffer offset
loop searchit ; otherwise repeat maximum cx times
mov di,01 ; if we get here we didnt find one
xor es,es
foundit:
dec di ; di was pointing one byte beyond actual address
mov ax,es ; get segment offset
mov bx,di ; store address for start of buffer
lea di,addrss ; heres where we will put the address
stosw ; store segment in variable
mov ax,bx ; get address for start of buffer
stosw ; store offset in variable
pop es
popa
; that was it ############################################################
converttostring:
pusha
lea si,addrss ; convert value
lea di,string ; to hex string
mov cx,4 ; 4 byte value
call hexstringproc
popa
printresult:
pusha
mov ax,03h ; mode number
int 10h ; bios call
mov bx,5 ; get x value
mov dx,5 ; get y value
lea si,string ; get string offset
call textproc
popa
pauseforkey:
xor ah,ah
int 16h
ret
hexstringproc proc
xor ax,ax
lodsb
ror ax,04h ; mov lower number from al to ah
shr ah,04h ; repair al
add ax,3030h
cmp ah,"9"
ja hexstringah
hexstringcheckal:
cmp al,"9"
ja hexstringal
hexstringstore:
stosb
mov al,ah
stosb
loop hexstringproc
mov al,"h" ; hex terminate
stosb
xor al,al ; zero terminate
stosb
ret
hexstringah:
add ah,07h
jmp hexstringcheckal
hexstringal:
add al,07h
jmp hexstringstore
hexstringproc endp
textproc proc
; find length of string
mov di,si ; string address
pusha ; preserve registers
cld ; clear flags
xor al,al ; compare zero
mov cx,-1 ; compare maximum 0FFFFh times
repnz scasb ; compare until both are zero
not cx ; invert cx
dec cx ; point cx to char before the zero
mov es,cx ; conserve value in es
popa ; restore registers
mov cx,es ; restore value
mov di,si ; string address
; prepare offset for co-ordinates
push 0B800h ; screen text buffer
pop es
mov ax,160 ; screen width in bytes (80 words/chars)
mul dx ; convert to offset
add ax,bx ; x co-ordinate
add ax,bx ; double x (for word)
mov di,ax ; load finished offset
; print it to screen
mov ah,0Fh ; colour attributes
textloop:
lodsb ; load next char
stosw ; store char & colour
loop textloop
ret
textproc endp
tooooo easy to be considered a challenge when reading a byte using lodsb es:di already holds address of the byte in question
Quote from: Hotshot on March 29, 2007
float mysteryFunction3D_1(vector_3d_ptr u,vector_3d_ptr v)
{
return( (u->x * v->x) + (u->y * v->y) + (u->z * v->z));
}
what the hell kinda operator is "->" supposed to be but it looks somewhat like a 3 dimensional crossproduct if "->" is actually a minus
my mistake thats no crossproduct
Assembly 1 - MysteryFunctionAsm()
addr address of the buffer to be written to
value 1 is the number of bytes that should be copied
value 2 is the byte that gets copied
basically drop double copies of [value 2] into buffer specified by [addr] repeatedly 1/2 [value 1] times
I've not coded in C/C++ for like 8 years :p
C.6.
returns the log10 of n
3D.1.
|5 2|
|6 10|
3D.2.
|19 12|
|11 6|
3D.3.
returns the scalar|dot product of the vectors u and v
3D.4.
|1 0 0|
|0 1 0|
|0 0 1|
3D.5.
?
int function mul16_16( int a, int b )
{
return a*b>>16;
}
3D.6.
Won't go into details but the indea of a BSP, Binary Space Partionning, tree is to chop the space/scene recursiverly in half spaces ( which might involve splitting some triangles in the scene ) to optimize the traversal of the said scene. It's handy to find what faces an observer at any given point and direction.
WEB.1.
does an XHR done in JS qualifies :p
Quotewhat the hell kinda operator is "->" supposed to be
That's a pointer. (And it's a dot product).
Say you have
typedef struct
{
int x, y;
} point;
int main(void)
{
point a;
point *b;
int c;
a.x=5;
a.y=6;
b=&a; /* b is now the address of a */
c=b->x; /* b->x is retrieved from a */
/* c is now equal to 5 */
return 0;
}
C/C++
2.
virtual ~A() { delete [] a; }
~B() { delete [] b; }
Anyone else a C++ fan? Did I get the virtual in the right place?
3. I know you can write to constants in constructors.
CConstant(int initial_value)
{
m_constant = initial_value;
}
6. is 1+n/10
3D
5. You have to worry about overflow and precision.
/* if 64bit longs are available */
FIXED mulfixed(FIXED a, FIXED b)
{
return ((__int64)a*b)>>16;
}
/* if floats are available */
FIXED mulfixed(FIXED a, FIXED b)
{
return (FIXED)(((float)a*b)*(1.0f/65536.0f));
}
/* hard way */
FIXED mulfixed(FIXED a, FIXED b)
{
FIXED top32;
FIXED mid32;
top32 = (a>>16)*(b>>16);
mid32 = (a&0x0000ffff)*(b>>16)+(a>>16)*(b&0x0000ffff);
return top32+(mid32>>16);
}
someone want to check that?
Quote from: Jim on March 29, 2007
C/C++
2.
virtual ~A() { delete [] a; }
~B() { delete [] b; }
Anyone else a C++ fan? Did I get the virtual in the right place?
Yup. It is in the right place Jim. If you sometimes struggle where to put the
virtualkeyword - it is also no problem putting it in front of both destructors. But keep that
in mind if there is another class C e.g. which is derived from B.
It's times like these when you realize how little you really know about your programming language...;D
My problem lately has been that C++ hadn't even been standardised when I was at university, so I have never had a C++ course. All the new programmers at my work have done C++ at some stage in their education. So I'm having to teach myself, which isn't a problem for me, specifically, since I use the language every day. It's just when someone asks me a question or to explain something about the language, I'm really not that confident.
Jim
In my university the main language was Java. We had some basic C and ASM courses as well
though. Nevertheless, we didn't focus on a certain language anyway. The focus was
to get to know all the design patterns and steps of software architecture and pro-
ject planning. The actual programming language was secondary then ...
But, as far as I can judge it - your explanations are very good here, Jim !!!
Absolutely - the point is not what languages you know, it's the discipline of computing and software engineering that matter. That's what's pissed me off recently - the pre-interviews I've had have had questions like the OP's in them and that is totally irrelevant to my skills.
We did Modula-2 at university, and Eiffel. How useful was that to getting a job?
Jim
Thats fuckin stupid they are so busy tryin to cover all the bases that they figure you will pick up the most important stuff yourself. I never went to college myself I learnt to program through trial and error its the best way I think you might pick up some bad habits by doing it but over time your strengths outweigh them
Hey that sucks jim. I'm studying e-commerce at the moment but doing the C++ module from the standard IT degree just cos I want to be able to show that I have some experience with the language.
Still its not going to prove that I can do anything useful with it. I just have to make one console app using inheritance and dynamic objects. Then sit a multiple choice exam. I'm sure you would find this all piss easy and it wouldent take you long either. Maybe you could do something similar? Then at least you will have something to shove in the next dumb employers face.
lol i must be getting better i got 2 questions correct :updance:.
the 2dcrossproduct one and the matrix addition :D
I taught myself C on the Amiga while I was at university - in fact, I had to code my dissertation in C because the Sun workstation I was given to use didn't have Modula-2 on it, and I've taught myself C++ on this job. I don't particularly want a certificate to shove in someone's face. If that's what they are looking for, then they are not looking for me, they're looking for a piece of paper :)
Jim