# Unity C# Interview test "failed"? Why?

This topic is 572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is the test with my answers. You guys have any pointers on how I could have done this better or techniques I can learn?

Better Formatted test is attached

Interview Test -

Questions:

1. In C# what is the difference between a class and struct?

Class uses reference types so if you change a variable in the class somewhere it will change for all references to that variable.

A struct will only change that iteration of the variable and will not be referenced anywhere else.

Classes are allocated on the stack while structs are allocated in the heap.

For example accessing a struct variable in a list that you have elsewhere will not change in the original struct but a class's variable will.

2. In what is operator overloading? In C# what are some of the operators that can be

overloaded? Name some that can not?

Operator Overloading allows you to change the functionality of the standard operators such as +,-,==,++. It allows you to add parameters to the operators and uses a return type.

you cannot change operators such as '=', '?', '.' and others.

3. What does the following function do?

bool dosomething(unsigned int x) {

return x && !(x & (x­1))?

}

return x makes sure x != 0

!(x & (x-1)) checks if x is a power of two.

So it returns true if x is a power of two.

4. Given two arrays A1 and A2. Where A2 has n elements and A1 has m elements plus

extra space for n elements. Both arrays are sorted in ascendant order. Write a function

to merge the two arrays into A1. The merged array must also be in ascendant order. We

want to do this without allocating or using a swap buffer or extra array.

void merge(int[] A1, int[] A2) {

// A1 has extra space for A2.

}

{

// Which index in array 1 will relate to spots for array 2.

int lastSpace = array2.Length;

lastSpace = array1.Length - lastSpace;

// Quickly Merge the 2 arrays.

for (int i = 0; i < array2.Length; i++)

{

array1[lastSpace] = array2;

lastSpace++;

}

// Use QuickSort to quickly sort (this is redundant) the arrays using arrays already given, no buffer no extra arrays used.

QuickSort_Recursive(array1, 0, array1.Length - 1);

}

int Partition(int[] numbers, int left, int right)

{

int pivot = numbers

;

while (true)

{

while (numbers

< pivot)

left++;

while (numbers

> pivot)

right--;

if (left < right)

{

int temp = numbers

;

numbers

= numbers

;

numbers

= temp;

}

else

{

return right;

}

}

}

void QuickSort_Recursive(int[] arr, int left, int right)

{

// For Recusrion

if(left < right)

{

int pivot = Partition(arr, left, right);

if(pivot > 1)

QuickSort_Recursive(arr, left, pivot - 1);

if(pivot + 1 < right)

QuickSort_Recursive(arr, pivot + 1, right);

}

}

5. Write a function that takes a string as an argument and returns a new string that is

the reverse of the string passed in.

C#

string ReverseString(string inString)

{

// This can also be accomplished manually by getting length of string and putting it one by one

// into char[] using for loop.

char[] reverseString = inString.ToCharArray();

// This is a C# function, useful here but can easily be done in a loop.

// To do that I'd just hold the character i'm swapping, swap it and put it at place I swapped from until midpoint.

Array.Reverse(reverseString);

return reverseString;

}

6. Write a function to traverse a binary tree composed of the following structure. Print

the contents of "name" at each node.

struct TreeNode {

public string name?

public TreeNode leftnode?

public TreeNode rightnode?

}?

// Unity code for printing tree node names in a left first recursive order.

void PrintBinaryTree(ref TreeNode treenode ) {

Debug.Log(treenode.name);

if (treenode.leftnode != null)

{

PrintBinaryTree(treenode);

}

if (treenode.rightnode != null)

{

PrintBinaryTree(treenode);

}

}

7. Write a function to sort an array of integers in ascending order. Do not use

Array.sort or similar built in functions.

// Just going to use a recursive quicksort again as it is the best way to do this. It's relatively fast.

void SortArray( int[] inarray ) {

QuickSort_Recursive(inarray, 0, inarray.Length - 1);

}

int Partition(int[] numbers, int left, int right)

{

int pivot = numbers

;

while (true)

{

while (numbers

< pivot)

left++;

while (numbers

> pivot)

right--;

if (left < right)

{

int temp = numbers

;

numbers

= numbers

;

numbers

= temp;

}

else

{

return right;

}

}

}

void QuickSort_Recursive(int[] arr, int left, int right)

{

// For Recusrion

if(left < right)

{

int pivot = Partition(arr, left, right);

if(pivot > 1)

QuickSort_Recursive(arr, left, pivot - 1);

if(pivot + 1 < right)

QuickSort_Recursive(arr, pivot + 1, right);

}

}

8. Given an up and right vector, reconstruct the forward vector from this data and

ensure the result is a vector of unit length.

struct Vec3 {

public float x?

public float y?

public float z?

}?

public Vec3 CalculateForwardVector( Vec3 up, Vec3 right ) {

// reconstruct the forward unit vector

// C# Implimentation

// Cross products of vectors create the perpendicular. The perpendicular of up and right is forwards / back depending on first parameter (up or right).

Vector3 forward = Vector3.Cross(up, right);

forward = Vector3.Normalize(forward)

return forward;

// This Implementation

Vec3 forward = new Vec3;

// Cross the vector

forward.x = up.b*right.c - up.c*right.b;

forward.y = up.c*right.up - up.a*right.c;

forward.z = up.a*right.b - up.b*right.a;

// Normalize the Vector3 to make it unit vector.

float length = sqrt((forward.x * forward.x) + (forward.y * forward.y) + (forward.z * forward.z));

forward.x = forward.x / length;

forward.y = forward.y / length;

forward.z = forward.z / length;

return forward;

}

9. Given an object with a position and direction, translate the object along the direction

vector as defined by "distance".

struct Vec3 {

public float x?

public float y?

public float z?

}?struct Obj {

public Vec3 position?

public Vec3 direction?

}?

public void Translate( ref Obj theobject, float distance ) {

// translate theobject by distance along it's direction

Vec3.Normalize(direction);

position += direction * distance;

theobject.position = position;

}

Choose one of the following for the 10th question:

// I did both because both intrigued me.

10.?You are a player standing at point P in 3D space. An enemy AI fires a weapon from point

E. The bullet follows a straight line and terminates at point H where it hits an object. If the

bullet's path comes within distance D of the player, then the game should play a "bullet whiz"

sound. Please outline an algorithm to determine wether the game needs to play that sound.

// Using Unity style code (C# / Mono)

// Get distance / direction vector of two objects.

Vector3 direction = playerObject.transform.position - bulletObject.transform.position;

// get distance via magnitude of direction vector

float distance = direction.magnitude;

// Play the sound if distance is less than or equal to the distance required to play sound.

if (distance <= soundDistance)

PlaySound();

// Also in unity you can just use a sphere collider trigger and layers to cause this.

10. ?Your boss comes to you in the 11th hour and says that QA has just discovered that the

new shout out feature of the application is allowing people to enter profanity this is then

posted to all registered users. He needs you to come up with a solution that will filter only the

profane word(s) out of the entered text and replace it with the string "(bleep)". On top of that,

he needs to be able to maintain this in some way when new words are to be added to the list

of words to filter, and he doesn't want to provide the words in "leet speak" but he does want

that filtered as well. So given the word "test" he would like words like the following replaced as

well: "t3st", "te5t", "t35t". Please outline a system to determine weather the entered "shout

out" string contains profanity, how to replace it, and to be expandable to allow continual

inclusion of new words and "leet speak" substitutions to check for.

C# version

I also did a simple version of this for my game because people were using obscene user names.

I used an XML list that I imported, checked every login (to see if people changed their names).

You can then create a dictionary for leetspeak characters and check each letter and convert any leetspeak to

regular text then do a simple string.contains(string) to check for obscenities.

Edited by Twinnuke

##### Share on other sites

4. Given two arrays A1 and A2. Where A2 has n elements and A1 has m elements plus
extra space for n elements. Both arrays are sorted in ascendant order. Write a function
to merge the two arrays into A1. The merged array must also be in ascendant order. We
want to do this without allocating or using a swap buffer or extra array.

I don't know any C#, but reading a bit through your answers, they probably expected a faster implementation for merging two sorted arrays. For instance, you could do something like this (in C/C++, but probably very close to being valid C#):

int read_index_1 = m - 1, read_index_2 = n - 1;
for (int write_index = m + n - 1; write_index >= 0; --write_index) {
else
}


##### Share on other sites
I like that. I'm going to try it. I'm almost positive the code would work the same since it's basic types.

##### Share on other sites

It seems like you're familiar with programming concepts, but you don't check to make sure your code actually works, and you frequently use the wrong terminology or answer things in cumbersome ways.Throughout the interview, the questions look like they're mixing C++ and C#. They put semicolons after structs (you don't need that in C#). Problem 3 would compile in C++ but not C# (C# doesn't have "unsigned int" - it has uint - and uints cannot be cast to bools implicitly). For problem 6 they totally Frankenstein'd that struct definition - that sucker won't compile in C++ or C#.1. Your use of terminology is off a bit. You got the heap/stack allocation part wrong.2. Your terminology is way off but it feels like you have the right idea.3. That function will not compile in C#. Might be an interviewer mistake or a trick question.4. Sorting problems are too painful to eyeball quickly.5. Your function does not compile.6. C# structs cannot be recursively defined, the problem is a trick question. If it was a class instead and actually compiled, your implementation has typos and would stack overflow due to infinite recursion. You also wouldn't need to use 'ref'.7. Sorting problems are too painful to eyeball quickly.8. Your code won't compile. a, b, and c?9. Your code won't compile. direction is not defined in your function's scope.10a. I think they mean that the bullet's path is a one-frame line segment, and they want you to see if the player is close enough to the line segment.10b. Sounds reasonable. XML is overkill though.

I created no structs on my own they were given like that. I knew the ? Return answer would not compile in c# and I also knew that the tree sorting did not work properly due to their struct. But I couldn't hand the test back with 3 blank answers and blame it on them.

They told me this was for unity / c# mono so my unity code did work I tested it. I feel like the person who made this test did not ask the right questions for the job and it irritates me.

##### Share on other sites
As regarding your input it's very helpful. I did get the terminology wrong. I need to know that for interviews even though it's not super relevant to me actually programming in unity.

Also yeah the direction was part of a struct. I must have overlooked that and saw it as part of the scope not in a struct. I fail.

##### Share on other sites

If you get more tests where the problem looks like it isn't correct, do three things:- Leave a comment at the start of your answer about the question possibly having a typo in it, but don't be snarky like I was. Programming interviewers come in two flavors: Complete assholes who will resent you for pointing out mistakes, and people who like the fact that you can spot errors. Pointing out a mistake to an interviewer needs to be done cautiously since you don't know what kind of person they are.- Solve the problem as if it were not a trick question - try to fix the question first, then solve it.- Make sure your code compiles and runs properly! If it's a test where you have access to a computer, there's no reason not to make a working program. If it's on paper, then it's a bit more excusable. :)

I did for most of them and did notice that 3 & 6 wouldn't compile and that vec3 isn't a c# thing as we have a native vector3 class. I feel this test was written for c++ and misjudged my c# skill set.

##### Share on other sites

...and that vec3 isn't a c# thing as we have a native vector3 class.

Vector3 is a Unity-specific thing (well, other game frameworks like XNA have similar vector types).

I wouldn't call any of them native - C# and .Net's core libraries don't have any built-in vector types that I know of. The Windows-specific class libraries like WinForms and WPF have vector/point classes, but I wouldn't consider them to be native either.

##### Share on other sites

Make sure your code compiles and runs properly!

This more than anything.  Most likely they have someone with an exist set of tests, they copy/paste your code into their compiler, run it, and verify the results.

Nypyren and others already gave some good responses, I'll throw in some extra comments.

#4 you add combine the two arrays then re-sort. Performance-wise that can be a terrible thing, especially on long arrays. There are many established algorithms for it that are covered in data structures classes. You might use the Merge Sort algorithm for for guidance, as that is exactly what half the algorithm does.

#5 is a common question. There are many different solutions and reasons for them. You gave one, the built-in library, with a comment "I could have done it the other ways", and that is a terrible response. In an interview it is hard to claim you can do things other ways and then not do them, but on the other hand, with some experience and a bit of tact you can get away with that.  Generally this question deserves at least three answers.  The first is as you gave, the built in library, with a brief description of why it should be preferred.  The second should be the xor swap, coupled with a description of why that swap is incredibly awesome on some architectures, why it suffers from abysmal pipeline stalls on some architectures, and the curious status on the x86 family ever since the out of order core was introduced. The third answer should be an implementation of reversing in place that demonstrates you can manipulate memory addresses.

#6.  That's... well... I'm not sure how you thought that was a good answer. Combined with question #4 it makes me wonder how much you actually learned in your algorithms and data structures courses.

#8. Looks like you copied part of the answer from one web site, and part of the answer from another web site. Sometimes they are .a, .b, .c, other times .x, .y, .z.

#10. Read the question more carefully.  They're looking for the minimum distance between the line segment and the point.

I feel this test was written for c++ and misjudged my c# skill set.

Did you feel that way before you took the test and failed it?

I can imagine those same questions, or very slightly modified questions, being asked in just about any language to judge your ability to write game code.

1. What is the difference between the stack and the heap, and perhaps explain a little about memory management strategies.

2. Explain one common feature of the language. This could be nearly any common feature in any language.

3. Do you understand bitwise operations?

4. Do you understand the basics of algorithmic complexity? Do you understand how to interleave data? Do understand a variety of algorithms?  Will you pick the O(n) algorithm or do like you did, the O(n) + quicksort with a potential worst case of O(n^2)?

5. Can you work with pointers and memory indexes? Do you understand performance ramifications of those choices?

6. Can you work with classic fundamental data structures?

7. Can you work with classic fundamental algorithms?

8. Can you recite some basic linear algebra?

9. Can use actually use some basic linear algebra?

10a. Can use use some intermediate linear algebra? In this case, the minimum distance between a point and a line.

10b. Can you use some intermediate other algorithms? It could be solved with various options, such as regular expressions, replacement algorithms, even searching algorithms like A* by treating permutations as search distances in a way similar to spell checkers.

None of those questions or answers are particular to any specific language.

Read your answers as though they asked these ten intention questions instead. Basically the 'book knowledge' intention of the question.  Judging by question intent, #1 is backwards, #2 is reasonable but could be expounded on, #3 is reasonable, #4 is very incomplete, #5 is wrong, #6 is wrong, #7 I didn't bother to check so I'll say it is right for this, #8 is close but questionable, #9 is incomplete, #10a is the answer to a different question, #10b is probably good.  So 4 good answers, 4 questionable or too short answers, 3 flat out wrong answers, and one answer where you didn't understand the question. Not too good.

Besides generic book knowledge, they also want to verify that you can write code that runs well.

One difficulty with hiring people is that employers want to filter out all the terrible programmers quickly. Some people can quote books and talk about algorithms but cannot write code at all. An inexpensive method for that is to just give you a standardized set of programming problems.  They can glance over the code, throw the answer into a pre-built test runner, and validate the results. If all the code runs cleanly, look it over for a simple code review to see if you implemented the algorithms well or not, and make a decision.

Considering that type of test, it is pretty clear that your answers don't compile cleanly, don't give the right results, some of them are fatally flawed.  So that part would fail the code writing test.

Put them together, and they'll likely find quite a few applicants who can score better on their test.

##### Share on other sites

This topic is 572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015210
×