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 & (x1))?
}
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.
// your code
}
{
// 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.