You are on page 1of 3

Emergent Programming Evaluation

Please take some time to consider and answer the following 5 questions based on your
programming knowledge and experience.
• If you need to make certain assumptions in order to answer a question, please
state your assumptions thoroughly.
• Please show all of your work, including code, documentation, and math, and
explain why your answers are correct.
• If you have difficulty answering a question, work through it as far as you can.
• Correctness, efficiency, and maintainability are all important.
• Good luck and we hope you enjoy answering the questions.

Question 1:

Given three points A, B, and C in 3D space, write a simple C++ function to find and return
the cosine of the angle between ray AB and ray AC. The points are stored as the
following class:

class Point3
{
public:
float x, y, z;
};

Question 2:

Write a C++ function to convert an unsigned short to a character string that contains the
value expressed as a hexadecimal string. Standard library functions like sprintf,
while valid and preferable in the field, are not what we are looking for. The function
prototype is as follows:

bool UnsignedShortToHexString(
unsigned short usValue, // The value to convert
char* pcDestBuffer, // The destination string buffer
size_t stMaxBufferSize) // The maximum size of the string buffer

The output boolean value is whether or not it was possible to fit the converted value within
the size of the input buffer. If it was not possible to fit the value into the provided buffer,
the input buffer is not to be modified.

Examples:
Input Input Return
Output String
Number Buffer Size Value
1 7 True "0x0001"
15 10 True "0x000F"
31 7 True "0x001F"
4096 7 True "0x1000"
1 6 True "0x001"
4096 6 False unchanged
10 4 True "0xA"
17 5 True “0x11”

Question 3:

You've been asked by a co-worker to perform a code review. What suggestions or


comments would you provide about the code below:
0 unsigned int FindNthOccurrenceFromEnd(const char* buffer, int
nthOccurrence, char testChar)
1 {
2 unsigned int inputLength = strlen(buffer);
3 unsigned int testCount;
4
5 for (unsigned int ui = inputLength; ui >= 0; ui--)
6 {
7 if (*(buffer) == testChar)
8 {
9 ++testCount;
10 if (nthOccurrence = testCount)
11 return ui;
12 buffer--;
13 }
14 }
15
16 return 0;
17 }

Question 4:

The speed of memory access in modern architectures is growing much slower than the
speed of arithmetic processors. Therefore, reducing the size of objects can lead to
more efficient cache utilization and faster code. The following class contains data
for an automobile. Rewrite the class to make its internal data representation as
small as possible. Describe these changes and justify your decisions including
any time/space tradeoffs, making sure to state any assumptions that you make
about the class' usage.

Consider the following points when you rework the class. Primitive types must be aligned
on the target machine. int types are 4 bytes in size. bool types are 1 byte.

class Automobile
{
protected:
// The top speed in miles per hour.
unsigned int m_uiTopSpeed;

// A boolean indicating if the car is moving or not


bool m_bIsMoving;

// The current speed of the car.


int m_iCurrentSpeed;
// The weight of the car in pounds.
unsigned int m_uiWeight;

// A boolean indicating if the car is currently started.


bool m_bEngineOn;

// A boolean indicating if the car has its head lights turned on.
bool m_bHeadlightsOn;
};

Question 5:

Assume that you have a game with the following basic main loop:

while (1)
{
DoInput();
DoPhysics();
DoAI();
DoAnimation();
DoRender();
DoSound();

After profiling the code you learn that each of the subsystems takes the following number of cycles to
execute:

Subsystem Cycles/Frame
Input: 5,000
Sound: 10,000
Physics: 30,000
AI: 45,000
Animation: 25,000
Rendering: 60,000

If you only have a budget of 100,000 cycles per frame, what are some approaches you might consider in
order to meet the 100,000 cycles per frame budget? Imagine you are in front of a whiteboard with the rest
of the team. What are the potential pitfalls of any given idea? What are the strengths?

You might also like