Q: What is Big O (time complexity) of finding an element in hash table?
Q: What is Big O (time complexity) of sorting an array?
O(n log n)
I had to look up answers for these two questions. I now (hopefully) understand big O to be a notation used to estimate an algorithm's relationship between the size of its input and its performance (speed in this case but could also be used to estimate disk space or another resource).
I have studied mathematics and algorithm design very little. However, I am confident that I can learn anything I need to learn to solve a problem, especially when test-driving a solution.
As an example, here is some code I wrote while "discovering" how to write a minimal perfect hash function. Within the specified range this algorithm will achieve avalanche and have zero collisions. The library is my Rails plugin called obfuscate_id. Here is the specific code I am refering to.
Q: How do you check CPU usage of a process on Unix system?
I usually reach for
top when manually checking out CPU usage. Checking the man page for
ps I see I can also do
ps -p <pid>.
Although I would probably do it differently today, here is a method I wrote about four years ago in a script that runs on all of my company's virtual servers:
def get_load result = `cat /proc/loadavg` result = result.split @data[:load_1m] = result.strip @data[:load_5m] = result.strip @data[:load_15m] = result.strip end
The script collects this and other data periodically and sends it via json back to our monitoring servers.
Today I would write something using higher level tools. For monitoring individual processes I like using Monit.
Q: Explain how HTTP redirect works?
Instead of sending the requested document to a server, the server can respond with a URL in the location header like this,
Location: http://example.com. The browser will make the request again from the new URL. This is usually transparent to the end user.
I have often used 301 redirection in Nginx and other webservers when changing a page's URL to minimize SEO impact.
Q: What is the difference between "has_and_belongs_to_many" and "has_many through"?
has_many :through describe a many-to-many relationship between two ActiveRecord models using a third join table.
has_many :through is a more powerful feature that gives you access to all the features of an ActiveRecord model on the join table.
has_many :through was first introduced I quickly found myself converting all my hacked up
has_and_belongs_to_many relationships to
has_many :through. I haven't come across a need to use
has_and_belongs_to_many since then.
Q: What is a Mix-In in Ruby?
A way to share behavior between multiple classes.
I typically use Mixins to share a set methods that support a specific behavior between multiple unrelated classes. In Ruby you can stick those methods into a module and include them in each of the classes that you want to have that behavior. This removes any need for multiple inheritance which Ruby does not support. The syntax to include a module's methods in a class is
Q: What does 'devise' gem do?
Devise is primarily an authentication library. I use Devise in most of my Rails apps these days that need a user login. It is built on top of Warden which lets me use a single authentication strategy in multiple applications via Rack Middleware. Although Devise can support multiple roles, I think authorization is best handled by something else, (like Cancan).