LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2022, 02:56 AM   #1
SarthakJain
LQ Newbie
 
Registered: Jul 2022
Location: India
Posts: 3

Rep: Reputation: 0
Got these questions in an interview today.


var a = 10;
function foo(){
console.log(a,b);
var a = 20;
var b = 30;
c = 40;
}
foo();
console.log(a, c)

--------------------------------------------------

console.log([1,2,3,4,5,6].map(e => e < 3))

--------------------------------------------------

const a = [1,2,3];
a.push(4);
const b = a[5]
console.log(a,b);

--------------------------------------------------

for(var i = 0 ; i < 5; i++){
setTimeout(function (){
console.log(i);
},0)
}
console.log(i).


Can anyone Put there views on this? what can be the probable answers for this.
Thanks in advance
 
Old 07-26-2022, 06:50 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,985

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120

If those are the test questions, I guess the position was for a entry-level JavaScript developer.

With a browser in front of you, determining the actual output should be trivial - if you're not yet capable of that, you need to start at the beginning - maybe check MDN for learning resources / tutorials.

If the issue is understanding why the behaviour is as it is, you need to understand scopes and search MDN for any keywords/functions you don't understand.

 
1 members found this post helpful.
Old 07-26-2022, 08:53 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 25,407

Rep: Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575
Quote:
Originally Posted by SarthakJain View Post
Code:
var a = 10;
function foo(){
  console.log(a,b);
  var a = 20;
  var b = 30;
  c = 40;
}
foo();
console.log(a, c)

--------------------------------------------------

console.log([1,2,3,4,5,6].map(e => e < 3))

--------------------------------------------------

const a = [1,2,3];
a.push(4);
const b = a[5]
console.log(a,b);

--------------------------------------------------

for(var i = 0 ; i < 5; i++){
  setTimeout(function (){
   console.log(i);
  },0)
 }
console.log(i).
Can anyone Put there views on this? what can be the probable answers for this.
As stated by boughtonp, these weren't interview questions, but a sample test. And not to sound nasty, but if you can't answer them or don't understand the questions, you're not qualified for the job.

If we tell you the answers here, what's going to happen if you get the position?? Are you just going to ask forums everywhere how to do the job you just got??? And your own profile says:
Quote:
Originally Posted by SarthakJain
Hi, I’m Sarthak. I’m a Computer Science and Engineering graduate who is passionate about programming and technology.
So you're a CS and Engineering graduate, who is 'passionate' about programming....and these stumped you??

Last edited by TB0ne; 07-26-2022 at 02:10 PM.
 
2 members found this post helpful.
Old 07-26-2022, 11:56 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,009
Blog Entries: 4

Rep: Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607
I dunno ... I just don't like "interview test-questions." I never did. I often thought that the people who wrote them were just trying to be clever. What I really want to know is how a candidate would view a problem Ė what his initial thoughts as to an approach might be. If he does not know the particular vagaries of a particular language, he can find it out quickly enough and/or ask for assistance. But, can he think?
 
2 members found this post helpful.
Old 07-26-2022, 09:58 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
Well, I assume the point of the interview questions were to see if you knew Javascript well enough to predict the output just by looking at the code. Now that the interview is over, all you need to do to actually find out is to paste that code into your web browser's Javascript console.

Last edited by dugan; 07-27-2022 at 12:02 AM.
 
1 members found this post helpful.
Old 07-27-2022, 09:49 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,009
Blog Entries: 4

Rep: Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607
Honestly, @dugan, I just think that questions like these are – at best - "edge cases." No one should ever write code like that, so they might not know what such code would do.

I remember, many decades ago now, when a vendor of a debugging tool would publish code snippets in their [magazine ...] advertisements and challenge the reader to predict what the code would do. While teaching a programming class at the local community college, I actually handed out copies of one of those ads and said: "Don't write code like this!"

"Code like this" is "clever." And it almost instantly becomes indecipherable, even if you wrote it. When I go back to look at source-code that I myself wrote just a few months ago, I completely rely on my comments, written at the time, and on "simple" coding practices that I try to make "stupid obvious." Cleverness is not my friend; nor should it be yours.

Ummm ... #undef SOAPBOX

Last edited by sundialsvcs; 07-27-2022 at 02:46 PM.
 
2 members found this post helpful.
Old 07-27-2022, 11:29 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,736
Blog Entries: 13

Rep: Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784
Looks like pretty fundamental code to me there.

I don't prefer "trick" interview questions intended to trip candidates up.

If a candidate doesn't understand code, show an ability to learn, show any thought process, or debug abilities, then it's on to the next candidate.

I also find it very difficult to believe these were the only questions, or instead the hiring company provided a first screen page like those questions to save wasting time before they got into more difficult topics.
 
1 members found this post helpful.
Old 07-27-2022, 11:55 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 25,407

Rep: Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575Reputation: 7575
Quote:
Originally Posted by rtmistler View Post
Looks like pretty fundamental code to me there.

I don't prefer "trick" interview questions intended to trip candidates up. If a candidate doesn't understand code, show an ability to learn, show any thought process, or debug abilities, then it's on to the next candidate. I also find it very difficult to believe these were the only questions, or instead the hiring company provided a first screen page like those questions to save wasting time before they got into more difficult topics.
Agreed...I think it's more likely they're homework questions, to be honest.
 
1 members found this post helpful.
Old 07-27-2022, 04:55 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,736
Blog Entries: 13

Rep: Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784
Quote:
Originally Posted by SarthakJain
Hi, Iím Sarthak. Iím a Computer Science and Engineering graduate who is passionate about programming and technology.
I missed this from TB0ne's post earlier.

Whether or not you think any initial questions may be too difficult or unfair, you haven't said this, but think about this.

You are likely seeking software engineering roles or something close, salaried positions of high value compensation.

Do you (hopefully not) have a misperception that companies wish to bring in someone who can't program in a primary language which they most likely cited as a requirement for the role?

As a new graduate they realize you will be learning, employers very much will expect that you have absorbed your lessons from college, and further, gone beyond it potentially. The candidates who stick out and get jobs sooner and obtain higher offers, demonstrate that they can, and have, taken their lessons further. Like they didn't get stuck and stopped, instead they dug deeper and learned more than the average student.
 
2 members found this post helpful.
Old 07-28-2022, 01:47 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,009
Blog Entries: 4

Rep: Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607
I was a member of the second class that could earn a "Computer Science" degree at my local University. Yes, I was one of the guinea pigs, although I didn't know it at the time. But, computers were (and still are ...) my hobby, and I basically taught myself.

But, these days, the technology is "arguably, bewildering." You cannot possibly "know all of it," and even if you could do so today, tomorrow would inevitably be different. So, "if you are any good at this," today you are able to quickly learn whatever it is that you need to know, and apply it.

This is what I meant when I said, "But can you think?"

Very interestingly, I have found for myself that I learn whatever I need to know, but then forget it. I can look back on archives of projects done even a year ago and now it seems like "the work of a complete stranger." I recognize that the comments were of course written by myself, but I must re-learn what they meant. (Which is precisely why I write comments on-the-spot and with such detail, capturing my immediate thoughts at that instant.) As the hokey song says: "It's all com - ing back to me now ..."

Last edited by sundialsvcs; 07-28-2022 at 01:49 PM.
 
Old 10-22-2022, 06:48 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
One thing that strikes me about these questions: two of them deal with the non-intuitive behavior of "var", and would be more straightforward if "var" were replaced with the newer "let" keyword.

Anyway, anyone who wants to try them out can just paste them here:

https://replit.com/languages/nodejs

Last edited by dugan; 10-22-2022 at 07:05 PM.
 
Old 10-23-2022, 12:47 AM   #12
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,689

Rep: Reputation: 530Reputation: 530Reputation: 530Reputation: 530Reputation: 530Reputation: 530
Quote:
Originally Posted by sundialsvcs View Post
I remember, many decades ago now, when a vendor of a debugging tool would publish code snippets in their [magazine ...] advertisements and challenge the reader to predict what the code would do. While teaching a programming class at the local community college, I actually handed out copies of one of those ads and said: "Don't write code like this!"

"Code like this" is "clever." And it almost instantly becomes indecipherable, even if you wrote it. When I go back to look at source-code that I myself wrote just a few months ago, I completely rely on my comments, written at the time, and on "simple" coding practices that I try to make "stupid obvious." Cleverness is not my friend; nor should it be yours.
This ^^^^^^^^^

It's the same reason I cringe when I see someone struggling to be clever with a one-liner -- in any language -- when a much clearer implementation is going to work out in the long run. As you said that "source-code that I myself wrote just a few months ago" can turn out to be pretty opaque when you come back to it to tweak it or borrow a bit of the code to use elsewhere. If the task requires doing something "clever", document the heck out of that code---you won't regret it.
 
Old 10-23-2022, 05:23 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,250

Rep: Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525
Quote:
Originally Posted by rnturn View Post
This ^^^^^^^^^

It's the same reason I cringe when I see someone struggling to be clever with a one-liner -- in any language -- when a much clearer implementation is going to work out in the long run. As you said that "source-code that I myself wrote just a few months ago" can turn out to be pretty opaque when you come back to it to tweak it or borrow a bit of the code to use elsewhere. If the task requires doing something "clever", document the heck out of that code---you won't regret it.
This is an interesting question. If you want to do your best (because of performance or any kind of optimization) probably you will write code which cannot be easily understood by others. Because you are an expert and you can do it much better.
The question is: should you write your code for dummies (just to make it maintainable and understandable for everyone) or fine tune it to the limits (and probably dummies will not even understand your comments and will not be able to extend it - only if they also become experts).
 
Old 10-23-2022, 10:16 AM   #14
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,985

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Quote:
Originally Posted by pan64 View Post
The question is: should you write your code for dummies (just to make it maintainable and understandable for everyone) or fine tune it to the limits (and probably dummies will not even understand your comments and will not be able to extend it - only if they also become experts).
False dichotomy.

Code written by good/senior developers ("experts") is more maintainable and understandable - by all developers - than code written by mediocre/mid-level developers and new/junior developers. (It is mid-level developers that are often the problem, having learned enough to be dangerous; new developers tend to lack the knowledge/experience to write overly convoluted code, and if they do it generally holds little value and can be scrapped/rewritten.)

Code should not be targetted at new/junior developers ("dummies") - otherwise those developers will never improve and the software will remain as if written by newbies - but that is not at all the same as producing a cryptic mess of obtuse one-liners with poorly named variables.

In the rare situations where code is necessarily dense/unclear, experienced developers already know how to make use of the available options to ensure clarity in what it does and how to handle any issues it might have, and will want to do so.

 
3 members found this post helpful.
Old 10-23-2022, 12:19 PM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,250

Rep: Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525
Quote:
Originally Posted by boughtonp View Post
False dichotomy.

Code written by good/senior developers ("experts") is more maintainable and understandable - by all developers
That is unfortunately not true. Beginners use only part of the language elements and also they do not understand everything. Therefore what you wrote is just a wish (more maintainable is true, by all developers is false).

Furthermore code is not written to improve the skills of beginners. That is just nonsense, the real goal is to implement a feature (and probably make money).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please answer these two interview questions crahuldba@gmail.com Linux - Newbie 9 06-27-2011 12:30 PM
I've got questions, you got answers? cynicalpsycho Linux - Newbie 16 03-20-2009 08:57 AM
getting updates today is IMPOSSIBLE (today)!! mtdew3q SUSE / openSUSE 3 10-17-2007 01:53 AM
amazing job interview today jukebox55 General 7 09-07-2007 12:09 PM
So I was at this interview today... bkeating General 2 07-12-2002 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration