3

I'm currently studying for a technical interview and I'm just going through the leetcode grind. I came across a question that is apparently asked pretty frequently by the company I'm about to interview at so I attempted it. I couldn't quite get it so I looked to the solution and came across this solution.

var merge = function(intervals) {
    if(!intervals.length) return intervals;
    intervals = intervals.sort((a,b) => a[0] - b[0])
    
    let prev = intervals[0];
    let res = [prev];
    
    for(let curr of intervals){
        if(curr[0] <= prev[1]){
            prev[1] = Math.max(prev[1], curr[1]);
        } else {
            res.push(curr);
            prev = curr;
        }
    }
    
    return res;
};

on line 5, res is set to equal [prev], which in this case is [1,3] so

res = [[1,3]]

for now.

Then as the code progresses, notice how the value of prev is updated. I thought this was weird since res is never updated at all, but by updating the value of prev, the prev inside res is also updated. My question is:

How does updating the value of prev update the value of the prev that's inside res? I thought I was going crazy so I tested it and this solution worked. I thought that once the prev inside res has been assigned, it would be immutable? Am I missing something here?

leed38
  • 314
  • 1
  • 4
  • 14
  • [Please read this](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) and also [this](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing). – evolutionxbox Dec 19 '21 at 00:16
  • 1
    "*How are variables referenced?*" is the wrong question, or at least, demonstrates your misconception. There are no references to variables in JavaScript. What you might want to ask is "*How are **values** referenced?*" – Bergi Dec 19 '21 at 00:27
  • An aside... these nonsense interview code samples give me a headache. :-) I get that this is just some simple data manipulation sample, but do you ever wish they'd do something more real? And, the mix of `var` and `let` drives me nuts, hahaha. – Brad Dec 19 '21 at 00:29
  • @Bergi ya I wasn't really sure how to phrase this question so I just kind of typed out what I felt would be most correct and hoped for the best. Brad I agree with you but I'm going to assume it's because they don't know what team you'll be working on when you apply so they just give you data structures/algorithms to see if you're up to snuff. This is for an internship position. just a random thought but SO gets a really bad rep for being dismissive of questions but I don't think I've really run into a problem like that here before, so thank you all for the insight! – leed38 Dec 19 '21 at 00:31
  • @Brad - It's like deciding whether someone can be a doctor by asking them to pass a sewing test. It only indicates something useful if they *can't* do it, IMO. – danh Dec 19 '21 at 00:32

3 Answers3

3
let prev = intervals[0];
let res = [prev];

At this point, res[0], prev, and intervals[0] all have the same value.

console.log(prev === res[0]); // true
console.log(prev === intervals[0]); // true
console.log(res[0] === intervals[0]); // true

If this value were referencing an object, then they all point at the object. If you were to reassign prev, that does not modify res[0], nor intervals[0].

prev = 'something else';
console.log(prev === res[0]); // FALSE
console.log(prev === intervals[0]); // FALSE
console.log(res[0] === intervals[0]); // true

How does updating the value of prev update the value of the prev that's inside res?

You can see by this example that updating the value of prev does not actually update the value of res[0].

However, updating the value of prev[1] is actually updating the value of index 1 of the object/array that prev references. In other words, you're not changing prev by setting prev[1] =... you're changing prev[1], which is the same as res[0][1].

Brad
  • 159,648
  • 54
  • 349
  • 530
2

This happens because prev's reference is being pointed by res when you do res=[prev], basically the address where the actual prev array is stored is pointed, as the prev updates, it also show changes in res.

Daniyal Shaikh
  • 419
  • 3
  • 12
2

It doesn't make sense to you because you're assuming that a new copy of prev is being created when you do res = [prev].
Instead, try to think of it this way: an array gets created which internally points to the memory address of the data that prev contains. Whenever data in that memory address changes, it will be updated everywhere that address was used.
I would read up about reference types vs primitive types.

Aziz Yokubjonov
  • 656
  • 6
  • 9