Alright, the final regret in the job interview that completely blew it for me. So to recap I spent roughly 40 minutes on the previous problems getting increasingly nervous and frustrated during the entire thing. My brain as basically gone at this point, and I was working with binary trees which I don't have any direct experience with dealing with. So all around this last question was going to be a good time. So here it is , the final function that had to be implemented
That's it. That's all it was, a simple delete tree function. It iterates through all the elements of the tree and deletes them. So having a mental breakdown at this moment in time it takes me a few minutes to pump out some terrible code that uses recursion.
Still not told if we we need to allocate and destroy the C string at this point. Still doesn't really matter, since it's just a minor implementation detail. Note to those reading is that my solution at that time was not this clean and I messed it up severely multiple times before getting to that solution. But alright, that code works. The interviewer wants a non-recursive version of it. Because like we said earlier, we don't want the overhead of a bunch of recursive calls.
This bent me over and did my raw, at that moment I knew I was screwed, I had struggled with the recursive version and I'll completely blow this one. At this point I may as well as have been a stump, I was a fool, a mess, I was on the outside looking in. Screaming to myself mentally to calm down and think it through logically.
I couldn't, I was done. Ten minutes of me mumbling to myself and stuttering I hadn't had anything to show. The interviewer pitied me and gave me the answer instead. It's incredibly simple and clean, it hurt me more than any words could.
It made complete sense, the interviewer asked me if I understood how it worked. I cringed, I understood it completely. I fumbled out a few words saying I did plus some other unintelligible nonsense at the time. I had lost completely, there was nothing I could do. This is my regret, and it is one of the largest in my collection. If I redid the interview now I would still be a nervous mess just because of the companies reputation and have had wanting to work there since I was a child. I completely blew it with this company, and for that I regret.
Code:
void delete_tree(node* root)
{
// Use c++ delete
}
That's it. That's all it was, a simple delete tree function. It iterates through all the elements of the tree and deletes them. So having a mental breakdown at this moment in time it takes me a few minutes to pump out some terrible code that uses recursion.
Code:
if (!root)
return;
delete_tree(root->child_left);
delete_tree(root->child_right);
delete root;
Still not told if we we need to allocate and destroy the C string at this point. Still doesn't really matter, since it's just a minor implementation detail. Note to those reading is that my solution at that time was not this clean and I messed it up severely multiple times before getting to that solution. But alright, that code works. The interviewer wants a non-recursive version of it. Because like we said earlier, we don't want the overhead of a bunch of recursive calls.
This bent me over and did my raw, at that moment I knew I was screwed, I had struggled with the recursive version and I'll completely blow this one. At this point I may as well as have been a stump, I was a fool, a mess, I was on the outside looking in. Screaming to myself mentally to calm down and think it through logically.
I couldn't, I was done. Ten minutes of me mumbling to myself and stuttering I hadn't had anything to show. The interviewer pitied me and gave me the answer instead. It's incredibly simple and clean, it hurt me more than any words could.
Code:
std::vector<node*> elements;
elements.push_back(root);
while (elements.size())
{
auto it = elements.back();
elements.pop_back();
if (!it)
continue;
elements.push_back(it->child_left);
elements.push_back(it->child_right);
delete it;
}
It made complete sense, the interviewer asked me if I understood how it worked. I cringed, I understood it completely. I fumbled out a few words saying I did plus some other unintelligible nonsense at the time. I had lost completely, there was nothing I could do. This is my regret, and it is one of the largest in my collection. If I redid the interview now I would still be a nervous mess just because of the companies reputation and have had wanting to work there since I was a child. I completely blew it with this company, and for that I regret.