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?
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…
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).
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;
}
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.
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.
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