LinuxQuestions.org
Visit Jeremy's Blog.
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, 01: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, 05:50 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,376

Rep: Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429

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, 07:53 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,109

Rep: Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836
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 01:10 PM.
 
2 members found this post helpful.
Old 07-26-2022, 10:56 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,357
Blog Entries: 4

Rep: Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820
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, 08:58 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,073

Rep: Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258
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-26-2022 at 11:02 PM.
 
1 members found this post helpful.
Old 07-27-2022, 08:49 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,357
Blog Entries: 4

Rep: Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820
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 01:46 PM.
 
2 members found this post helpful.
Old 07-27-2022, 10:29 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,813
Blog Entries: 13

Rep: Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875
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, 10:55 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,109

Rep: Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836Reputation: 7836
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, 03:55 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,813
Blog Entries: 13

Rep: Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875
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, 12:47 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,357
Blog Entries: 4

Rep: Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820Reputation: 3820
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 12:49 PM.
 
Old 10-22-2022, 05:48 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,073

Rep: Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258Reputation: 5258
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 06:05 PM.
 
Old 10-22-2022, 11:47 PM   #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,744

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
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, 04:23 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,764

Rep: Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053
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, 09:16 AM   #14
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,376

Rep: Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429
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, 11:19 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,764

Rep: Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053Reputation: 7053
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 11:30 AM
I've got questions, you got answers? cynicalpsycho Linux - Newbie 16 03-20-2009 07:57 AM
getting updates today is IMPOSSIBLE (today)!! mtdew3q SUSE / openSUSE 3 10-17-2007 12:53 AM
amazing job interview today jukebox55 General 7 09-07-2007 11:09 AM
So I was at this interview today... bkeating General 2 07-12-2002 10:09 AM

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

All times are GMT -5. The time now is 04:50 PM.

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