Any JavaScript developers out there willing to help me out with something?

I have an interview question that I like to ask candidates that no one ever seems to get right. But, to me, it seems pretty basic, so I expect MOST JavaScript developers at almost any level of expertise to get it, and I like it generally because it demonstrates some core knowledge of JavaScript concepts and syntax.

But I want to verify that my feelings about it are reasonable, because give how few ever seem to get it right (and I'm talking across literally hundreds of interviews, MAYBE 2 people have ever gotten it right), I'm starting to wonder if I'm right or not.

Look at this code, and then answer the question after. Please do so off the top of your head and without testing anything since that's normally the experience a candidate would have. I'll give the answer after some time for anyone who gets it wrong but is curious.

But this isn't about YOU getting it right or not, and it's not about whether it's the best way to do something in JavaScript or anything like that, it's just about whether it's a reasonable question and whether my expectation that MOST JavaScript developers should get it right is fair.

const O = {
sayHello : function() { alert("Hello"); }
const S = "sayHello";

Question: using ONLY the variables O and S (and you MUST use both), write code that executes the sayHello function.


  • 13
  • 3
    @Cyanide But you also used square brackets, parentheses and a semi-colon. The deal was “only the variables O and S”...
  • 3
    @platypus I have to ask: is your reply a joke? I don't mean this insultingly, I'm sincerely asking. Because if candidates are getting tripped up by "only the variables O and S" thinking they LITERALLY can only use those letters and not basic JavaScript syntax as well, then that's a legitimate problem in my question (and one I'm not sure I'd ever have guessed).

    Of course, if it's a joke, then haha, well-done :)
  • 3
    Why the fuck would any frigging sane person do that....
  • 1
    @IntrusionCM Do what? (not that it really matters for our purposes here because I specifically said I'm not concerned with whether it's a good idea or not, but now I'm curious how someone would have such a strong reaction)
  • 4
    @fzammetti what you want is in a nutshell a validation that the person knows basic JS syntax and it's object handling plus variables of variables.

    What you validate is something a person should be able to learn in less than 5 minutes and the test is hence kinda pointless. Instead of the test by code, rather ask if he can think of a solution and let him explain step by step how he would tackle that problem and see if he at least understands that the problem exhibits a relationship between a function the variable of a variable...

    If the person gets that, you know his problem solving approach and that said person is not a moron.

    No one would write code like this in any real scenario and letting someone write code in a stress test is - in my opinion - the wrong filter. Many people, including myself, might fail such a test although we are not morons.... Either because feeling offended (too easy) or a general anxiety / hate regarding being tested.

    Code tests are bad, mhkay. XD
  • 2
    @IntrusionCM Doesn't the fact that SO many candidates can't seem to answer this show that filtering basic JavaScript knowledge like this is a valid thing to do?

    As for no one would write this in the real world, on that I 100% disagree. I've seen it several times, and I know I've written it once or twice myself. I would agree that it's probably not common, and probably only makes sense in very limited circumstances, but unless you see no value in ensuring someone understands concepts like object literals and object property access, then it seems like a fair question to me.

    I ask plenty of "how would you"-type questions as you say, but I've always felt this is a good, more concrete question.

    I'm not really understanding your objection... if you're offended by too easy a question then that strikes me as a weird issue, just answer it and move on. And anxiety/hate over being tested, well, you're in an INTERVIEW for a position that REQUIRES JavaScript programming, what do you expect?
  • 1
    @fzammetti It’s a little tongue-in-cheek, but also intended to make a point about the use of toy problems.

    One might assume it’s a test to see if you can follow a set of requirements accurately without getting sidetracked by fun coding problems.

    Yes, that would be a pedantic interpretation...but programming is a pedantic business.
  • 3
    @fzammetti I'd expect to be treated like a human person, not a stupid monkey who can reproduce code questions you can learn by heart from too many sites in the INet.

    And that's kinda the point.

    If the interviewer uses filtering based on scoring (eg. by code reproduction), I guess it's not experience that is wanted, but rather being a little drone.

    And as I said... There are many reasons why you might fail this test. Neither need to be that you are a bad programmer.

    I know some geniuses who are so introvert that the mentioning of interview gives them PTSD.

    Get to know them, give them time... You suddenly have an amazing, heartful person who writes algorithms from heaven.

    It's mostly the companies own fault if they only get shitty programmer, and one of the most prominent reasons for that is that they try to shorten the interviewing process by scoring.
  • 1
    @platypus That's fair, thank you. Based on this, I think I'm going to modify the question slightly to make sure that doesn't happen (something like "using only the variables O and S and basic JavaScript syntax", along those lines). I might also literally say that this is a coding problem and not a comprehension problem (not directly, at least). I know once I had someone give me the answer "O.sayHello.call(null);" and even though that's technically wrong given the question, I considered that a perfectly good and valid answer. Even when someone gives me a "wrong" answer, I always think about if they're telling me something good about their abilities indirectly, or if maybe they gave me a good answer to a different question because they didn't quite understand what I was asking, which also can be a positive thing (I also would not hold it against someone who got this question truly wrong if the rest of the interview showed they know their stuff - no one gets EVERY question right).
  • 1
    Sometimes I do that kind of stuff but reading the exercise I wasnt able to answer.
    Then by accident read the first comment. So I dont even know if I would be able to do the exercise :/
  • 2
    This question is good, if only to remind me why I hate javascript so much.
  • 1
    @nibor Haha, that's fair :)
  • 1
    @WorstVarNames Did you find the question confusing in some way, or not asked well? Aside from any debate about whether it's a good question or not generally, and understanding that I'm very much of the "even good candidates don't get EVERY question right" mentality when I interview, if I'm going to ask this question, I want to make sure I'm doing so as well as possible so I'm not getting people answering wrong just because I didn't ask the question well enough.

    I should note that I also always give the person the answer afterwards "just to satisfy their curiosity", as I usually put it, and usually I get something like "oh, duh, right, that makes sense", which is another reason I wanted to get some opinions, because that sort of implies maybe the question isn't as clear as it seems to be to me.
  • 2
    I dont get what „core knowledge“ you are referring to. Its only a syntax question and doesnt offer deeper insight into how this person thinks.

    If only 2 out of 100 persons got it right you should have stopped asking the question after interview 10
  • 2
    @Crismon It demonstrates an understanding of the core structure of JavaScript objects. It demonstrates that you understand that objects are associative arrays and what that means in practice. By saying that you can only use those two variables, it means that you have to actually understand what you're dealing with, not just implementing a syntax pattern you've seen (call, apply, eval, etc.). I definitely disagree that it's only testing syntax. It clearly IS testing that too, but you won't get the syntax without understanding the concepts behind it. That so many get it wrong indicates I'm either asking the question poorly (which is entirely possible and is why I'm here), or that people don't have the fundamental knowledge they think they do, and my experience tells me it's (mostly) the latter, which is why I continue to ask it. For those who get it right, I KNOW they understand and aren't just regurgitating what they've seen.
  • 1
    I was thinking something like O.eval(S), or O.S, apparently I have failed :D
  • 3
    I like this question. Also, people fail this???? That is concerning...
  • 0
    @galileopy That's always been my take, but I want to make sure they aren't getting it wrong because I'm asking it poorly. The last dozen or so interviews have been over Teams, which has given me the opportunity to just paste the code and question into the chat and people still aren't getting it right, which is surprising to me.
  • 1
    @blindXfish O.S is one I've gotten pretty often. I don't remember anyone ever saying eval... though, the thing is, neither of those answers, while definitely wrong, are totally crazy. My thinking at that point would be that maybe you didn't understand the question, or your JS skills are just a bit weak. I could be wrong in both cases, but it would then come down to how the rest of the interview went. If the rest seemed to indicate to me that you knew your stuff, then I'd chalk that up as just a brainfart and it would be cool. No one question will ever disqualify someone for me... even a couple wrong, as long as you're in the ballpark, are okay with me. I'm not looking for perfection, not until I figure out how to attain it myself :)
  • 0
    I think its a bit trippy that you named the variables O and S. I have no idea what O and S stand for, that's not good variable naming and immediately puts me into "this is a trick question" mode.

    O[S](); looks so simple yet I wouldn't think of it when looking at O and S

    If the variables were literally called "obj" and "funcName" or something then it becomes trivial. It literally took me until I wrote this to figure out that O probably stands for Object, still not sure what S is supposed to mean.

    I feel this alone makes the question much more confusing, but fixing it will probably render it much less effective.
  • 1
    @fzammetti I really think you've answered it in this thread. It covers core concepts of Javascript, but at the same time, it feels like code that would be a straight up code smell (based on how the question is formatted) that a lot of developers would avoid doing such practice in the first place.

    You could probably keep it within the core concepts of JavaScript by naming two ways to access an object property and then giving a question asking them to how to execute the function defined in the object without using dot-notation.

    Personally, I would reword the question to be: "given the following Object <define object here>, execute the function *functionNameHere* without using dot-notation".
  • 0
    This question is pretty easy tbh. Took me a moment to realize the { } not to be a scope but an json object. Solved it like in the first reply without seeing that before.

    I'm not a JS programmer primarily but I really like such stuff. It's just interesting and proves a deeper understanding of programming in general.

    Vue and the likes offer you to specify your methods exactly this way (give or take some syntax sugar).

    Depending on the job, you may not need someone who realizes that. Many people can probably get the job done a d not realize this. Probably just a factor to consider.
  • 0
    @Hazarth Hmm, I see your point. I specifically used O and S (-O-bject and -S-tring) in an attempt to not give away the answer somehow, but obj and funcName I think are reasonable. I can't help but think someone should be able to get it either way, but I'm going to add that to my updates anyway. Thanks for that feedback!
  • 4
    I agree, anyone past newbie JS devs should know it.
  • 1
    That's a very basic application of JavaScript IMO and I got the answer instantly. You're either interviewing poor candidates or not explaining it as well as you did here
  • 1
    I'm not even a JS dev and I was able to figure out that it's probably O[S]. I wouldn't have gotten it if you didn't give the hints that sayHello is the name of the function and you must use both variables. Also, I knew that completely ridiculous things are possible in JS. Then, I used similar syntax to python dictionaries (which is why I missed the brackets).

    In an interview situation I probably wouldn't have figured it out.
  • 2
    I read this as an entirely reasonable question, any JS dev should know it. But I also get the issue with asking questions so basic that it trips people up because they might think "this can't be that simple, right?"
  • 3
    @lurch Yeah, good point. I'm thinking I might literally say "this isn't a trick question" next time I ask it.
  • 1
    @AlgoRythm Thanks. The first possibility, well, I get a mixture, I'll leave it at that :) The second possibility is exactly why I posted this. I've gotten some good suggestions and I've altered the question in a couple of ways, but because I agree that this is pretty basic, I'm still going to ask it. I have some interviews coming up this week, I'll be curious to see how it goes.
  • 0
    @fzammetti please update us eventually, I'm really interested to know if changing the variables names changes the average success rate at this answer.
  • 1
    @galileopy Will do. I have six interviews on the schedule for this week starting tomorrow, so I should at least start to get some idea by Friday.
  • 0
    @fzammetti I legit think that would help
  • 0
    This is kind of dumb question to ask. Two lines of code?

    You need something a decent sizes code block.

    Then ask them to walk you through the code.
Add Comment