Would like some understand on a ruby skills challenge I failed
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Would like some understand on a ruby skills challenge I failed
A while back, I was confronted with a skills challenge for potential employment. I failed it miserably. Its been quite some time since then and it is still haunting me.
So I tried to run through it again today. I figured without any time limit and for as much as I thought about it maybe I would have a better shot at getting it right or at least completed. Unfortunately not the case, so now I am looking for understanding on what I am lacking in my knowledge to get past this problem.
The challenge:
Get the minimum number of keystrokes to change one word to another word.
Example:
Code:
Original Word: "clarity"
New Word: "singularity"
The minimum number of keystrokes to change "clarity" to "singularity" is six (one to delete the "c" and five to type "singu").
The code I am working with to try and solve this challenge.
Code:
#
=begin
variabls:
original_word
new_word
processes:
Get a list of matching letters
Remove matching letters from both the original and new words
Get a count of remaining letters from the original word (count of letters to delete)
Get a count of remaining letters from the new word (count of letters to add)
Add both counts to get total number of keystrokes
output:
The minimum number of keystrokes to change "---" to "---" is six (n to delete the "-" and n to type "-----").
=end
original_word = 'clarity'
new_word = 'singularity'
original_word_array = original_word.split("")
new_word_array = new_word.split("")
ow_index = original_word_array.length - 1
nw_index = new_word_array.length - 1
i = 0
x = 0
matches = []
#### Having trouble incrementing through the characters of each correctly to separate the matching and non matching characters
while i <= nw_index do
if new_word_array[i] == original_word_array[x]
puts "MATCH"
matches << new_word_array[i]
i += 1
original_word_array.slice!(x)
x = 0
else
x = x == ow_index ? 0 : (x + 1)
i =
end
Also: don't get too up-tight about any job that gives you a "skills challenge" as a pre-condition of employment.
You can find much better jobs.
You should be able to describe to them programs that you have written, either in school or in previous employment, and you should be able to give references of "people who will speak well of you."
"People who will speak well of you" is the single most important thing that you must nurture and curry. Even an a*shole can be "a good, competent code-slinger," but (s)he will still remain "an a*shole," and in a team of people who are doing very exacting intellectual work under conditions of high pressure, they're a detriment to the team that must be avoided. A "skills challenge" won't tell you this.
If you've done any programming at all, I can teach you how to "sling code" the way we do. What I can't discern is whether or not you are an a*shole. Whether or not you will be a social(!) fit, and therefore an asset to the team.
- - -
Pondering this particular "challenge," I would consider it to be open to very broad interpretation and not at all obvious. I'm not persuaded that the algorithm proposed really does lead to "the right answer," and I can think of several disjoint possibilities of just what "the right answer" might be considered to be. It's fairly unreasonable to expect a candidate to just sit down and bang-out this thing in an interview situation.
Last edited by sundialsvcs; 04-04-2017 at 09:12 AM.
#!/usr/bin/env ruby
old='clarity'
new='singularity'
take = old.split(//).select{|c| new.sub!(c,'').nil?}.size
puts "Required number of moves is :- #{take + new.length}"
@cyent and @grail thanks for taking a stab at the challenge. The results that you both have written are specific to the 2 words that are in the example.
If I wanted to use 2 completely different words I don't think it would work. On a quick test in irb for @grail's code:
Code:
irb(main):009:0> old = 'car'
=> "car"
irb(main):010:0> new = 'bile'
=> "bile"
irb(main):011:0> take + new.length
=> 5
If my manual counting is right, the count should be 7 (3 to delete 'car', 4 to create 'bile')
But to be honest, even though I know my code was not right, I did the same thing. I wrote the code to satisfy the example words and not any 2 words.
I guess that goes into another thing I seem to run into which is having challenges that aren't directly clear about the expected results.
This so-called "challenge" appears to be quite specific to the two words given. It is certainly not a general statement of the problem, nor of a general solution to "all problems of this class."
Whether your solution – whatever it was – was "correct," would have been very much "in the eye of the beholder." You could not be reasonably expected to take on such a "challenge" in a pre-employment code-slinging contest.
Last edited by sundialsvcs; 04-04-2017 at 07:33 PM.
Not sure if you copied all of my data, but it does in fact return 7 for me using your new words. I did actually use half a dozen other combos including words with repeated characters.
I don't think it should matter, but I am on version 2.4
Actually having a quick look at what you have written, did you set 'take' variable again or is the 1 stored in that from previous run??
btw. I agree 100% with sundialsvcs that a test of this nature in an interview on the spot tells me little to nothing about you as an individual which I hold higher than your skills
which can be taught
@grail, I don't think it should matter. Your probably right, I might not have copied everything I apologize. The 'take' variable is a newly set variable from what I remember but honestly its been a couple of days and I should have posted the full IRB session for clarification .
@sundialsvcs, I am in full agreement with you on your thoughts. I will certainly read those links. I am very curious what they have to say.
As far as if the challenge was specific to those 2 words, that is part of what get me overthinking about these types of challenges. When seeing something as simple as the difference between 2 words, my first thought is to make it not just about those 2 words but any 2 words.
Just to be fair, this code challenge was part of the interview process where a cultural evaluation conversation was had. It wasn't a case of me showing up and them throwing me in a room to code. I have had other places start and end with code challenges before having any sort of conversation. At the time this challenge was given, that was not the case.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.