-
-
Save obliviusm/7d9e14dc470ce210c53cd7828303f7b6 to your computer and use it in GitHub Desktop.
ruby_interview https://www.toptal.com/ruby/interview-questions https://www.toptal.com/ruby-on-rails/interview-questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is the line of code below valid Ruby code? If so, what does it do? Explain your answer. | |
-> (a) {p a}["Hello world"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In what order will the numbers 1-4 be logged to the console when the code below is executed? Why? | |
(function() { | |
console.log(1); | |
setTimeout(function(){console.log(2)}, 1000); | |
setTimeout(function(){console.log(3)}, 0); | |
console.log(4); | |
})(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The values will be logged in the following order: | |
1 | |
4 | |
3 | |
2 | |
Let’s first explain the parts of this that are presumably more obvious: | |
1 and 4 are displayed first since they are logged by simple calls to console.log() without any delay | |
2 is displayed after 3 because 2 is being logged after a delay of 1000 msecs (i.e., 1 second) whereas 3 is being logged after a delay of 0 msecs. | |
OK, fine. But if 3 is being logged after a delay of 0 msecs, doesn’t that mean that it is being logged right away? And, if so, shouldn’t it be logged before 4, since 4 is being logged by a later line of code? | |
The answer has to do with properly understanding JavaScript events and timing. | |
The browser has an event loop which checks the event queue and processes pending events. For example, if an event happens in the background (e.g., a script onload event) while the browser is busy (e.g., processing an onclick), the event gets appended to the queue. When the onclick handler is complete, the queue is checked and the event is then handled (e.g., the onload script is executed). | |
Similarly, setTimeout() also puts execution of its referenced function into the event queue if the browser is busy. | |
When a value of zero is passed as the second argument to setTimeout(), it attempts to execute the specified function “as soon as possible”. Specifically, execution of the function is placed on the event queue to occur on the next timer tick. Note, though, that this is not immediate; the function is not executed until the next tick. That’s why in the above example, the call to console.log(4) occurs before the call to console.log(3) (since the call to console.log(3) is invoked via setTimeout, so it is slightly delayed). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given: | |
x = "hello" | |
Explain the difference between: | |
x += " world" | |
and | |
x.concat " world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Consider the following code: | |
class A | |
def self.a(b) | |
if b > 0 | |
b * b | |
end | |
end | |
end | |
What will be the values of: | |
var1 = A.a(0) | |
var2 = A.a(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Can you call a private method outside a Ruby class using its object? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What is the difference between Array#map and Array#each? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What is the primary difference in these two code snippets? | |
// Java | |
public boolean isEmpty(String s) { | |
return s.length() == 0; | |
} | |
# ruby | |
def empty?(s) | |
return s.size == 0 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What’s the issue with the controller code below? How would you fix it? | |
class CommentsController < ApplicationController | |
def users_comments | |
posts = Post.all | |
comments = posts.map(&:comments).flatten | |
@user_comments = comments.select do |comment| | |
comment.author.username == params[:username] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What paths (HTTP verb and URL) will be defined by the following snippet in config/routes.rb? | |
resources :posts do | |
member do | |
get 'comments' | |
end | |
collection do | |
post 'bulk_upload' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Suppose we have a Student with id=”4”. If we delete the Student with id=”4”, what will be the result of the following queries: | |
Student.find(4) | |
Student.find_by_id(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment