I took this developer test for a potential employer, and they said I failed because of my response to the first question. But Rails for Zombies seemed to imply that that's how you assign data to tables.
I'm aware that I am probably aiming a bit high for my Ruby skill level, but I refuse to not try.
Also, if you are a linear algebra master- feel free to try the last question. I know it involves markov chains and eignevalues, but I failed before I got there since the employer was watching me build the page.
Question #1 (Rails) If you are not familiar with rails, you should be able to answer these using the online documentation. Answers appropriate to Rails 2 or 3 will be accepted. Considering the following:
class Country < ActiveRecord::Base
has_many :cities
end
class City < ActiveRecord::Base
belongs_to :country
has_many :bars
end
class Bar < ActiveRecord::Base
belongs
How would you (in a controller method) assign to @country the Country named ‘France’?
Country.create(name: 'France')
How would you assign to @cities an Array of all the cities in France?
arrayOfFrenchCities.each{ |x| City.create(name: x) }
How would you assign to @bars an Array of all the bars in France?
barNamesInFrance.sort.each{ |z| Directory.create(name: z) }
Do any of the above answer change if there are 400 cities?
No, might take a few extra seconds.
How about if there are 20,000 bars?
No, might take quite a few extra seconds, and could be better read from a database than processed as an array.
Question #2 (Ruby) If you are unfamiliar with ruby, please use the documentation to answer this question. Let S be the set of numbers greater than zero and less than 100,000 that are evenly divisible by 19.
s = Array.new
s << 19
while (s.last + 19) < 100000 do
s << s.last + 19
end
How many numbers are there in S?
s.count
#=> 5263
How many numbers in S have a square that ends in a 1?
squaresEndingIn1= s.select { |num| num%10 == 1 || num%10 == 9 }
squaresEndingIn1.count
#=> 1053
How many numbers in S have a reflection that is also in S? (The reflection of 145 is 541)
reflections = s.select { |num| s.include?(num.to_s.reverse.to_i) }
reflections.count
#=> 280
How many numbers in S can be multiplied by some other number in S to produce a third number in S?
multiples= s.select { |num| (19 * num) < 100000 }
multiples.count
#=> 277
Question #3 (General) Use whatever language you are comfortable in to solve this problem. An ant is walking on the squares of a 5x5 grid - it starts in the center square. Each second, it will choose (with equal probability) to do one of the following:
- Move north one square
- Move south one square
- Move east one square
- Move west one square
- Do not move
If it cannot perform the action it has decided on (move west while on the west edge, for example), it sits in place. After one second, it has a 20% chance of being in the center, and a 20% chance of being in each adjacent square. (and a 0% chance of being in any other square on the board).
- What is the probability that the ant is on the center square after 15 seconds?
- What is the probability that the ant is on one of the outermost squares after 1 hour?
You may ignore floating point error accumulation.
@country = Country.find_by_name("France")
in the first question. I.e. retrieving, not creating. – Flambino Feb 10 at 22:11