Check out a free preview of the full Introduction to Data Structures for Interviews course

The "Stack: Edge Cases" Lesson is part of the full, Introduction to Data Structures for Interviews course featured in this preview video. Here's what you'd learn in this lesson:

Bianca discusses edge cases to consider in the stack data structure. Bianca then revisits an earlier point regarding type checking when pushing to the stack and what to do if pop() is invoked, but the stack is empty.

Preview
Close

Transcript from the "Stack: Edge Cases" Lesson

[00:00:00]
>> Bianca Gandolfo: So what are some things that could go wrong here? So for this one we had to check our input, make sure that it's the correct type, that it even passed anything. For this one-
>> Speaker 2: It's like the opposite of, what if there is an argument passed into it?

[00:00:18]
>> Bianca Gandolfo: Yeah, if there is an argument, nothing would happen.
>> Speaker 2: It's just it's not gonna be referenced, it's just gonna sit there.
>> Bianca Gandolfo: Yeah, it'll just disappear.
>> Speaker 3: Nothing's been added to this stack.
>> Bianca Gandolfo: Yeah.
>> Speaker 3: This stack it would be negative one, which would go back.
>> Bianca Gandolfo: Yeah, what if it is empty?

[00:00:35]
>> Bianca Gandolfo: Yeah, so how can we handle that? So if you're popping something that's empty, you would just, I guess we could either throw an error, or we could just do nothing, return undefined or something like that. So those are some things that you could do.
>> Speaker 2: Just wrap it into if statement?

[00:01:00]
>> Bianca Gandolfo: Yeah, so if you-
>> Speaker 2: If it's more than zero, then do this, if not, return.
>> Bianca Gandolfo: Yeah, exactly, so we can say if this._length. Yeah, so what this will account for, if you say if this._length, it will account for if it's undefined, or if it's 0. So just be careful about that.

[00:01:23]
It's good in some scenarios, but in some cases where you're checking if an array, or if like a what could also be undefined or 0? An index number could be 0, and that's a valid number, but you're checking if it exists or something like that. Just be mindful of this conditional catching undefineds, if that's not what your intention is.

[00:01:53]
Oops.
>> Bianca Gandolfo: So if we have something, do that, else whatever. You could throw, you could do nothing. Those are kinda your two options. However, this is where you'd ask your interviewer, do you want me to throw an error, or how do you want this implemented? And usually I would say, okay, why don't we see what an array does?

[00:02:32]
>> Bianca Gandolfo: So let's create an array. Let's make it a little bigger. And then, I forgot that I have a thing I can run. So if I say, arr.pop, it just returns undefined. So that would be my default assumption. It's just have it act like an array so that our APIs are similar, in so far that it doesn't compromise any of the other core behaviors of our data structure.

[00:02:58]
So the push and the pop and the NQDQ stuff,
>> Bianca Gandolfo: There's that.
>> Speaker 2: Cuz it automatically return undefined. We don't need the else statement?
>> Bianca Gandolfo: Yep, they'll automatically return undefined, exactly. Should we try it out?
>> Bianca Gandolfo: So now we push, push, pop, we're expecting it to look something like this.

[00:03:24]
Let's take a look, nice.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now