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

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?

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?

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

Arrays are Objects. Objects are stored by reference, which means that they are created in memory and what the variable actually “holds” is the address of that memory location. If I give you my home address, I am not giving you a house that exactly matches my own, just telling you how to find mine. When I give you that address, I am giving you the address to a yellow house. When I get the painters out this summer, your address will now be to a blue house. Or you could go to that address I gave you and paint it red. Then when I went home, I would go home to a red house.

3 Likes

Pen and paper are extremely important tools in effective programming! I’m not even kidding, sometimes you really do want to sit down with a notepad and a list of possible inputs and walk through every single line and execute the logic (as written, not as planned!) to make sure it works as expected. You also unit tests that do this programmatically. Sometimes I have to work for months or even years using only mocked-up versions of interfaces and unit tests.

1 Like

Btw, this function has one more thing that makes it lame - defaultValue should not be declared in the body of the function, but passed as argument instead. Feel free to bring it to the table with recruiter for couple of extra points :wink: