# Why did I fail this Ruby developer test? [closed]

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.

-

## closed as off topic by palacsint, Jeff Mercado, almaz, Donald.McLean, Jeff VanzellaFeb 12 '13 at 16:53

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Per the FAQ, actual code needs to be posted here. –  MECU Feb 10 '13 at 0:37
I was afraid of that, thanks for the tip. Updated. –  Silvius Feb 10 '13 at 1:08
I'm thinking the employer was looking for @country = Country.find_by_name("France") in the first question. I.e. retrieving, not creating. –  Flambino Feb 10 '13 at 22:11
Ahh I see. Thanks, Flambino. –  Silvius Feb 11 '13 at 1:47
And how did this question offend the critics enough to warrant a downvote? Good lord... –  Silvius Feb 11 '13 at 1:48

How would you (in a controller method) assign to @country the Country named ‘France’?

Country.create(name: 'France')


Err, no. You've created a new country, not found an existing country, and you've ignored the part about assigning it to @country. I think they were looking for something like this:

@country = Country.find_by_name 'France'


How would you assign to @cities an Array of all the cities in France?

arrayOfFrenchCities.each{ |x| City.create(name: x) }


Again, no, that's nothing like what they asked for. Something like this:

 @france  = Country.where(name: 'France').includes(:cities).first
@cities = @france.cities


How would you assign to @bars an Array of all the bars in France?

barNamesInFrance.sort.each{ |z| Directory.create(name: z) }


Still really far off, where did you get "Directory" from? Each question is building on the previous question. I'd try something like this as a nieve first pass:

 @france = Country.where(name: 'France').includes(cities: :bars).first

@bars = @france.cities.map(&:bars).flatten.uniq


Do any of the above answer change if there are 400 cities?

No, might take a few extra seconds.

I'm not sure what they're getting at; As written, no. You might consider an index on the Name column, but that doesn't affect the above answers.

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.

Again, you are correct in that "no", I wouldn't change my answer however your explanation makes no sense at all. What are you talking about, "processed as an array"? That's a complete non sequitur.

-