Table of Contents

Ruby theory questions

1. What are Ruby Gems?

This is a very open ended question and you might be better of to start with the basics first:

2. What is the difference between a Symbol and String?

3. What is the purpose of yield?

4. What are class variables? How do you define them?

5. How do you define instance variables?

6. How do you define global variables?

7. Does Ruby support constructors? How are they declared?

8. How can you dynamically define a method body?

FIXME

9. What is a Range?

10. How can you implement method overloading?

11. What is the difference between ''&&'', ''and'' and '&'?

FIXME: http://ablogaboutcode.com/2012/01/04/the-ampersand-operator-in-ruby/

12. How can you create setter and getter methods in Ruby?

Method Effect
attr_reader :v def v; @v; end
attr_writer :v def v=(value); @v=value; end
attr_accessor :v attr_reader :v; attr_writer :v

13. What is the convention for using ‘!’ at the end of a method name?

14. What is a module?

FIXME

15. Does Ruby support multiple inheritance?

16. How will you implement singleton pattern?

17. How will you implement an observer pattern?

18. How can you define a constant?

19. How can you define an Exception?

20. What is the default access modifier for a method? What are the differences between access modifiers?

21. How does .map() and .reduce() work in Ruby? What about .map(&:downcase) ?

What is new in Ruby 2.0?

TODO: http://magazine.rubyist.net/?Ruby200SpecialEn

What is new in Ruby 2.1?

Frozen String Literals

Required Keyword Arguments

Method definition returns method name

# Ruby 2.0
def foo() end # => nil
 
# Ruby 2.1
def foo() end # => :foo

Useful for metaprogramming.

Remove garbage bytes from Strings

"abc\u3042\x81".scrub("*") #=> "abc\u3042*"

Others

SEE: https://github.com/ruby/ruby/blob/v2_1_0/NEWS

23. What are the differences between Proc, lambda and block?

Blocks

Procedures

Lambdas

INFO:

24. Describe Ruby data structures and their advantages.

Array

Hash (or associative array, map, dictionary)

Set

Useful when:

INFO: http://spin.atomicobject.com/2012/09/04/when-is-a-set-better-than-an-array-in-ruby/

Struct

Use case:

OpenStruct

INFO:

25. What is the difference between equal?, eql?, ===, and == ?


Rails specific questions

1. Explain Rails MVC implementation.

2. What is a scope?

3. What deployment tools do you know? How do they work?

More info:

4. How can you migrate your database schema one level down?

rake db:rollback
rake db:rollback STEP=1
 
# Rollback to the beginning
rake db:reset
 
# or if you're not worried about data
rake db:purge

5. What is a sweeper?

6. How can you implement caching in Rails?

7. What is a filter? When it is called?

8. What is RESTful routing?

REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do). The promise is that if you adhere to REST principles while designing your application, you will end up with a system that exploits the Web’s architecture to your benefit. In summary, the key principles are:

9. What is the purpose of layouts?

10. Is it possible to embed partial views inside layouts?

11. How can you create a REST API for your application?

12. How can you define a new environment called 'staging'?

13. What is the difference between has_one and belongs_to?

14. What is a has and belongs to many association?

15. How can you implement single table inheritance?

16. What is a polymorophic association?

17. What is eager loading?

18. How does validation work?

19. Why is fields_for used for?

20. What is the purpose of a helper method?

21. What is flash?

22. How can you implement internationalization?

23. What is and how can you implement a state machine?

24. How can you upload a file to a server?

25. How can you show SEO friendly URLs instead of using only numeric IDs?

26. Is Rails scalable?

Yes Rails gives you complete freedom to use all traditional means of scaling an application. Things like memcached, caching full pages, caching fragments are all supported.

You can use any standard CDN to serve your media and static content as well.

Database scaling using sharding is supported.

Finally heroku makes your life easier by giving you the flexibility to scale up/down based on your need. Mostly websites have a peak time during which you need more servers and then there is a sleep time. Heroku makes that on-demand scaling process simpler. Companies such as HireFireApp.com makes the autoscale process easier.

27. What are the key deployment challenges?

28. How can you secure a rails application?

TODO: http://guides.rubyonrails.org/security.html

SQL injection

Use:

User.where("login = ? AND password = ?", entered_user_name, entered_password).first
# or
User.where(:login => entered_user_name, :password => entered_password).first

instead of:

User.first("login = '#{params[:name]}' AND password = '#{params[:password]}'")

because of:

SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'>'1' LIMIT 1 

29. How can rails engage with a SOA platform?

30. What kinds of associations are available in ActiveRecord

belongs_to

has_one

has_many

has_many :through

has_one :through

has_and_belongs_to_many

Polymorphic association

Self joins

31. Advantages of NoSQL (MongoDB) databases over SQL

32. What is new in Rails 4.0?

33. What is the difference between _path and _url route helpers?

_url methods generate entire URL, including protocol and domain, _path generates just the path part.

20.

General object-oriented and programming questions

1. What is polymorphism?

http://www.runtime-era.com/2012/08/polymorphism-in-ruby.html

2. What are SOLID principles?

Single Responsibility Principle

Open for extension, closed to modification

Liskov Substitution Principle

Interface Segregation

Dependency Inversion

Algorithm and data structure questions

TODO:

1. Reverse an interger, e.g. 12345 => 54321, without converting to a string.

def get_number_digits(number)
  digits = []
  modulo = 1
  loop do
    modulo *= 10
    digits << (number % modulo) / (modulo / 10)
    break if number < modulo
  end
  digits
end
 
def assemble_digits(ary)
  number = 0
  multiplier = 1
  ary.reverse.each do |digit|
    number += digit * multiplier
    multiplier *= 10
  end
  number
end
 
digits = get_number_digits(1923434)
assemble_digits(digits)

2. Peform a binary search on an array of numbers to find a number.

ARRAY = [1, 1, 1, 1, 5, 6, 8, 9, 10, 11]
 
def binary_search(ary, number)
  low_index   = 0
  high_index  = ary.size - 1
 
  while low_index <= high_index
    mid_index = (low_index + high_index) / 2
    value     = ary[mid_index]
    if value < number
      low_index = mid_index + 1
    elsif value > number
      high_index = mid_index - 1
    else
      return mid_index
    end
  end
  nil
end
 
puts binary_search(ARRAY, 1)

Notes:

3. What data structures are you familiar with?

4. Fibonacci

# From https://github.com/mruby/mruby/blob/master/benchmark/fib39.rb
def fib n
  return n if n < 2
  fib(n-2) + fib(n-1)
end
 
puts fib(39)

Real Questions

1. What kind of SQL Joins you know?

2. What is oAuth?

3. Find highest singular number from a given range.

4. What kind of HTTP actions do you know? How Rails uses them?

There are several described in RFC2616 for HTTP/1.1:

HTTP Verb Path Action
GET /photos index
GET /photos/new new
POST /photos create
GET /photos/:id show
GET /photos/:id/edit edit
PATCH/PUT /photos/:id update
DELETE /photos/:id destroy

5. How Rails simulates HTTP actions other than POST and GET?

Forms in web browsers can only send POST and GET requests. Any other request (PUT, DELETE) is send as a POST request with a hidden field called _method, set either to put or delete. Rails application detects it and route the request to update or destroy action respectively.

6. What is AJAX? What is it used for and what are the limitations of AJAX?

7. How would you implement Ruby Object#tap() method? How does it work?

class Object
  def tap
    yield self
    self
  end
end

It allows you do do something with an object inside of a block, and always have that block return the object itself.

INFO: http://www.seejohncode.com/2012/01/02/ruby-tap-that/

8. What is the difference between include and extend?

9. What is the difference between .find and .find_by_id in Rails?

10. What is the difference between after_commit and after_safe callback?

11. What is pJax?

12. If a class inherits from another class and a module, and they all have the same method, how the inheritance chain will look like?

How do you evaluate a gem for production environment?

There are a couple of criteria that should be considered when evaluating a gem:

How a singleton pattern is implemented in Ruby?

This is achieved by:

How is a Observer pattern implemented in Ruby?

FIXME: http://www.ruby-doc.org/stdlib-2.1.1/libdoc/observer/rdoc/Observable.html, https://github.com/ruby/ruby/blob/trunk/lib/observer.rb

Source

IRC

20:36 < jastix> sqbell: i was asked this kind of question only once. I was asked to think how to sort an array.

20:36 < dhoss> sqbell: do you know anything about basic data structures? sorting, searching trees, etc

20:36 < terrellt> sqbell: I'm not sure what they'd ask. If they're trying to find someone with a classic CS degree you're looking at hash tables, linked lists, dynamic arrays. Sorting algorithms. Most of that's 
                  dealt with in Ruby though.

20:37 < terrellt> Search trees are potentially useful in Rails land. Hash tables too.

20:38 < brownies> sqbell: at a bare minimum you should know your way around Hashes and Arrays (duh) and i'd also like to see knowledge of handy Ruby things like OpenStruct

20:39 < brownies> sqbell: from there it really depends on who is interviewing you and the company, but i would be mildly satisfied if you could then go on to talk about calling #map and #reduce on both of those 
                  things and how those work, and maybe i would make you write some basic algorithms using those methods

20:37 < joshuawscott> sqbell: if it's a Rails position, you'll likely be asked what you don't like about RoR.

20:40 < brownies> and perhaps since it's an RoR position that's a nice segue into talking about activeRecord things and DB structures

20:42 < jastix> sqbell: often people ask about fibonacci numbers and prime numbers

20:43 < joshuawscott> sqbell: http://codility.com/train/ is a good place to see what you don't know