Can't explain this interview question

Hello friends,

I recently got the below question as part of a written interview assessment (it specifically says that I can “feel free to use any available resources”):

// This function should create a 2 dimension array pre-populated
// with zeros.  The output appears correct, but it has a bug.
// Explain what's wrong and fix it.
function defaultMatrix(size) { // returns array
	// fix me!
	var defaultValue = 0;
	var row = [];
	var matrix = [];
	for (var i=0; i < size; i++) { row.push(defaultValue); }
	for (var i=0; i < size; i++) { matrix.push(row); }
	return matrix;
}

I’ve been looking at it for a couple of hours now, and I just don’t see the “bug”. I’ve tried playing with it on JSFiddle, Googled for the answer, looked on StackOverflow, even emailed the recruiter and asked if “this is a trick question there is no bug” was an acceptable answer. I’m at the point now where knowing the answer is more important than doing anything with the information. What am I missing here?

2 Likes

I’ll demonstrate a bug and it’s up to you to fix, ok? :slight_smile:

const matrix = defaultMatrix(5);
matrix[0][0] = 1;
console.log(matrix);
2 Likes

Here is a clue:

Interesting problem. So simple. I didn’t catch it right away but now that I see it, it’s so obvious what the problem is. It’s a good problem.

an other clue: a similar thing is also in the curriculum under the JS debugging section, not exactly the same, but similar enough

or look at it with this, should be able to show the issue:
http://www.pythontutor.com/javascript.html

And of course, less than an hour after writing this question, the answer just came to me.

2 Likes

@tlannoye11 Another example of something that “looks” right at first but does not work as expected is the following:

function abcs() {
  var arr = ['a', 'b', 'c', 'd', 'e'];
  for (var i = 0; i < arr.length; i++) {
    setTimeout(function() {
      console.log(arr[i]);
    }, 1000);
  }
}

abcs();

The intent of the abcs function is to have the letters a through e display one at a time 1 second a part. However, something is not right and causes unexpected results. Can you spot the issue(s) and correct the code? Try to figure it out without actually running the code.

1 Like

Hummm I can understand what is going wrong, but not WHY. It’s the same result as if row[0][1] was changed before being pushed into matrix, or if we had five declared “row” elements inside the matrix that shared the same value…

Also, pythontutor is overloaded right now.

1 Like

How many arrays are there?

this is really bugging me now. i don’t get it!

there’s one array made of five arrays? or so it appears?
feel like piccard with the cardassians

i can’t, i give up, what’s the answer?

Let me rephrase @ArielLeslie’s question differently.

How many row arrays are there?

Very subtle but a big bug indeed. Here are a few clues:

  • Array are passed by reference
  • Avoid to unintentionally change the original array by avoiding side effects
  • Clone vs copy

Hope that helps

This brings up a small tangent: why would I ever want to try and figure out anything “without running the code”?

1 Like

how ever many are passed to defaultMatrix() . if the parameter is 3. there are three rows
or maybe not?

how do you prove one way or another?

There is only a single array named row. It gets pushed to the matrix array as many times as the value passed to the function.

Sometimes you actually have to work under those constraints. With web programming we get used to being able to execute our changes immediately, but sometimes you actually have to go through a process of building a web application bundle, moving it around, and restarting an application. If you are working in a compiled language, you don’t want to rebuild your project every time you make a small change. Also, you may be writing a feature that relies on data from sources that you don’t have access to (such as a third party tool or an API that hasn’t been implemented yet).

how can you prove that? and how did you know that?

how can you debug Javascript without running it? what’s the method?

Now, I’ll post a fix and you tell me what do you think has been changed?

function defaultMatrix(size) {
	var defaultValue = 0;
	var row = [];
	var matrix = [];
	for (var i=0; i < size; i++) { row.push(defaultValue); }
	for (var i=0; i < size; i++) { matrix.push([...row]); }
	return matrix;
}
1 Like