Dolphin, the GameCube and Wii emulator - Forums

Full Version: My Regrets - Part {1, 2, 3}/3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Sorry you didn't get the job. Better luck next time though.

Anyway, was it sega of america by any chance? I googled popular tech companies out there and thats the only one I saw that made sense. o.o
(10-27-2014, 05:01 AM)rlaugh0095 Wrote: [ -> ]Sorry you didn't get the job. Better luck next time though.

Anyway, was it sega of america by any chance? I googled popular tech companies out there and thats the only one I saw that made sense. o.o

Whaaaat!? There are so many *more* though.
(10-27-2014, 03:57 PM)garrlker Wrote: [ -> ]
(10-27-2014, 05:01 AM)rlaugh0095 Wrote: [ -> ]Sorry you didn't get the job. Better luck next time though.

Anyway, was it sega of america by any chance? I googled popular tech companies out there and thats the only one I saw that made sense. o.o

Whaaaat!? There are so many *more* though.

Oh, I didn't see any of those. o.o
I actually misremembered in my first post about this being three questions long. It was actually four, but the two in this were fairly similar which is why I misremembered.
In any case, from the first question it was easy to determine that I was going to go in to a downward spiral from there. From this point on we are dealing with a basic binary tree. This is laid out in a very simple way with each node has two children and a string inside of it.
Code:
struct node
{
      char* name;
      node* child_left;
      node* child_right;
};

So the layout is really simple, basically the easiest binary tree representation one can make. So the first simple exercise to do this time around is to output the tree using regular C++ methods. The way the output is wanted is to first output the left child, then the current node, then the right child. So all we get is a function that gets a root node and spams all the output to cout. Here's what the function definition looks like.
Code:
// Example structure:
//        -"bar"-
//       /       \
//    -"foo"-   nullptr
//   /       \
// "baz"    nullptr
// prints string: "bazfoobar"
void print_string(node* root)
{    
}

Fairly simple right? Now there are a couple ways to go about this. We'll just show the one that I managed to generate in my stress filled hour of time.
Code:
if (!root)
      return;

print_string(root->child_left);

std::cout << root->name;

print_string(root->child_right);

Alright, top notch we did it. The code is simple and clean due to the recursion of calling itself. This is about what I got after a bit of fussing around. The only real issue with this code is the fact that we are using recursion. While it makes everything look nice, there is overhead in doing so due to having to build and destroy the callstack throughout each of the calls. Which if you're printing the binary tree you don't really care about speed anyway, so who cares. It may be something to worry about later though. If someone wants to whip up a way to do it without recursion for fun then have at it.

The next exercise is similar to the previous one, this time around we are prepending a string to the binary tree. So basically walk down the entire left side of the tree and stick it on to the very left. The function definition is as follows.
Code:
void prepend_string(node* root, char* name)
{    
}

Alright, so you get a root node and a character pointer. Some additional details are that the new node is generated using regular c++ new routines, and I wasn't told if we are required to generate new address space for the new name string. For the exercise I assume you didn't, this may have been a mistake but really it's a minor implementation details.
Code:
node* current_node = root;

for(; current_node->child_left; current_node = current_node->child_left) {}

current_node->child_left = new node;
current_node->child_left->name = name;

This is roughly the code I generated at the time. Albeit it was probably a fair bit dirtier at the time of writing during the phone conversation. During this point of the interview I was getting exceedingly frazzled and my mind was dying on me from nervousness. Again, I wasn't told if I needed to allocate memory for the new name so I just did an assignment. Which if anything it would be just a simple allocate and string copy.

So these two were fairly easy to do once again, I spent far too long implementing these due to being overly nervous. Probably didn't help that during this time a large amount of vehicles were driving by due to school being let out, and I had decided to do this interview outside a coffee shop. So the next post will be the final question that I completely balls up and wasn't even able to complete even though it was another easy exercise.
Is it bad that I've been taught so much functional programming, as opposed to regular programming, that I'm converting your code to ML in my head? It's giving me an aversion to reassigning variables, and iteration, and fixed datatypes (as opposed to polymorphic, not dynamic), and basically every other tool that makes making a computer do what you want really easy.
Should have initialized the child pointers in that new node.
current_node->child_left = new node();

Fix all the things Tongue
According to the given definition node is a struct (without a constructor), not a class.
nope, c++ magic: http://ideone.com/SgKiWt
edit:
also, since c++11: http://ideone.com/JkIyuW (although, it's cheating)
You don't want to make assumptions about the compiler in an interview. It wasn't established that this was C++ instead of C (I intentionally overlooked "node" being undeclared due to lack of a typedef) - use of char* rather than string and non-member functions smells like C...
Pages: 1 2 3