Learning From A Popular Interview Question
This is another post in the genre of technical interview questions I’ve come across during my...
For further actions, you may consider blocking this person and/or reporting abuse
I much prefer bit checks for that sort of thing, and I think it is valuable for beginners to at least know of this option because it opens the door to the concept of bit oriented shortcuts (there are a few worth knowing). Just wanted to add my two cents. But overall really good post!
Why do you much prefer bit hacking for that sort of thing? Is there a performance benefit or something?
For 99.9% of applications the modulus operator would be a better choice because people understand modulus immediately, and the performance trade-off (if there is one) is negligible compared to the benefit of immediate readability. There's a good chance most people will look at your bit hack and have to Google it or at least pause and think about it for a minute to understand.
That being said, it is a neat trick!
Yes, from what I understand the performance is better, though I think in the vast majority of cases it wouldn't matter. Here is a fun read about a real life problem solved with bitwise operations (not simple even odd stuff though...). I primarily mentioned it for the reasons I already stated, it is probably the simplest intro to bitwise operations that I know of, which opens the door for learning more about the language for a beginner. Also, if you have a "use a for loop to find all even odd values" interview problem, and you whip out that solution... Just sayin'
As for the real world webdev readability, I never really considered the &1 thing to be less readable than %2. In my case I had to google both the first time I saw them. AND I'll go as far to say, I have a hard time thinking outside of myself. What I mean by that, is I love seeing new stuff in code and learning what it is/does, so it is hard for me to think in terms of that bothering people (perhaps a weakness on my part). But I do understand the value of human readability over being clever (you can always just minify your code to make it smaller afterwards). But to me both the bitwise and modulus are an abstract symbol followed by a number, and you kinda have to learn what they do when you see them, just part of coding (a part that I enjoy).
But for the most part, I just threw it in the comments because it was a post for beginners and I thought that the target audience might enjoy it. Though I'm glad you commented on the immediate readability aspect, that is worth considering.
If you're talking optimization, having the if statement (branching) is the most expensive thing, because it requires the processor to dump and reload its entire register state, and kills off the ability for it to pipeline nicely. Getting rid of the conditional is by far the most optimal solution to this code.
💯
In general, I would say optimize for developer time over processor time. Developer time is so much more expensive these days... 😂
Lazy way:
(I call it utilitarian)
😂
Though if I asked you for all the odd numbers up to 100 or 1000 your way becomes way more tedious suddenly... 😉
This post is good for learning to delve deeper but risks teaching programmers to write code that tries to be too clever. Unless you're writing a library that needs to be highly optimized the original code will be preferable because the intent is much clearer. Favor clear over clever because someone is going to have to maintain that code down the line.
True.
That's the problem with many of these technical interviews, they seem to filter for clever solutions over clean, readable, and reusable code, which take very different skills to write.
This goal of this blog post was to help new developers acquire the skills needed to pass the interview. In real life, I would say the general rule is to optimize for developer time over processor time (depending on the situation of course), which means to write code that's easily readable and maintainable by future developers (including yourself).
I agree that there's nothing particularly clever about incrementing a counter variable by two in itself. I was just pointing out the risk of going down the clever code path. If I'm maintaining this bit of code that's part of a larger codebase I would want to see something that shows explicit intent like
There are other replies to the original post that are clearly going down the clever code path so I think it's worth mentioning.
You have a knack for breaking down concepts and really explaining them. Love reading your blog posts. Thanks for this!
Thanks!
21 iterations
extract
isOdd
out and you can now reuse it elsewhere. It also improves the readability of thefor
loop.isOdd
should not need to be a functionRuby has it as a function 😉
ruby-doc.org/core-1.8.7/Integer.ht...
ruby-doc.org/core-1.8.7/Integer.ht...
What if you were asked to do this without a for loop? ;)
While loop saves the day!
Or better yet ... use some functional programming!
in that case, see some of the other clever comments to the post :)
You can also do
console.log( [...Array(20).keys()].filter(n => {return n%2}).join(', ') )
Probably slower tho.
and you're done heh #vivapython
But that would make for a much shorter blog post... 😉
oh that's cool to see, thanks for sharing that.
I found this reply post in a stackoverflow thread on the matter
is for loop really needed here?
(filter #(not= 0 (rem % 2)) (range 1 21))
yeah, well, I posted it to support what you were showing, not as an argument. It was more just meant to be extra information than anything else.