Return curr if curr = nil or curr.next_node = nil Check it out in code: def reverse_list(curr) It’s a bit confusing because of the trick that the next of the current node actually ends up at the end of the reversed list so the next of the next of the current node is where we want to put the current node. Then, stick our current node on the “next” of this list. This will reverse the list and place the node that we passed in at the end of the reversed list. If not, pass the next node into our reverse function. If it is, then we just return immediately (i.e. Given a node, we first check whether or not the node is the last of the list. Basically, we solve this problem recursively. The solution is not immediately obvious, but becomes so once you’ve heard it a million times. This is a problem that’s so popular that pretty much no one uses it anymore. So, the code presented is made to be as simple and straightforward as possible. The idea is to write clear code that is easy to understand, which is often more important (especially in an interview setting) than using every possible trick available in the Ruby grammar. This article isn’t about how to write the most clever Ruby possible. So, by figuring out how to do the easy stuff in Ruby, you should be set to tackle the hard problems, too. Most of the time, harder interview questions aren’t hard because they involve really complicated code instead, they just require more thought and some insight. In this article, we’ll go through some reasonably easy to average difficulty interview problems using Ruby. But, there’s no reason why a Rubyist can’t work his/her way through interviews with our tool of choice. Most interview prep material seems to be either in Java or C++, primarily due to their ubiquity, as well as the fact that they don’t “do too much,” i.e. Although we can spend all day determining whether this is a good measure of programmer ability (it almost certainly isn’t, especially for companies whose primary business is building pretty simple iPhone apps), the deal is that learning to play the game is better (and certainly more lucrative) than avoiding it. At the core of the interview experience lie algorithms and data structures, i.e. However, they do tend to vary in difficulty and scope. As pretty much anyone who’s planning to do this knows, all companies ask, basically, the same flavor of questions. I spent some time this fall interviewing at various places across the industry.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |