I’m studying CTCI, chapter 3 is on stacks and queues. Seems super irrelevant to me considering javascript arrays are super flexible and have built in pop, push, shift, and unshift methods. So is going through chapter 3 worth my time for the faang interview or should I skip it? For the less aware, ctci = cracking the coding interview (book) Thanks for reading. - Long Shlong Style YOE:0 TC: O
Arrays can function as a Stack or a Q. Try to understand the differences of those data structures, and implement using the built-in functions you have mentioned.
I think a lot of languages have flexible lists or arrays, but companies still ask stack and queue questions.
Try implementing problems that can be done recursively iteratively. Or do post order traversal iteratively
Sweet thanks for steering me down the right path guys. I was thinking about skipping it but I definitely won’t now
Now the problem I’m supposed to solve is to describe how I could use a single array to implement 3 stacks, just want to make sure, are these problems relevant to JavaScript as well?
You really need to think beyond the language. A good interview question should be just as meaningfully answered with pseudo code. The language of choice is merely a tool to communicate what you know. If you just say "JS has this function built in so I don't know," that's not a very good sign from an intwrviewer's pov
Well I know queues are FILO and stacks are FIFO, and that the shift and unshift methods are linear time complexity while pop and push are constant. What more do I need to know?
I think you are approaching this the wrong way. An interview is not an exam. You don't prepare specific chapters because they will be asked about in the interview. You need to build a problem solving mindset and for this you need 3 things : - understanding of basics (data structures, design patterns...) - familiarity with common problems and their solutions - capacity to adapt known solution to novel problems For the last part, there is unfortunately no shortcut and it's only by doing that you will get better at it. I have been writing code for 20 years and I have never had a practical need to implement 3 stacks using one array (the solution is very basic btw) but solving this problem will add to your problem solving baggage (in this case, working with indexes) and will get you one step closer to becoming a better engineer. So unless you have a time constraint, I would not skip anything. In fact, even as a Javascript developer, I would strongly suggest you spend sometime honing your algorithmic and problem solving skills. It's a skill that will continue helping you grow throughout your career.
Thanks, I appreciate the response, I think I am just not understanding the relevance of stacks and queues. Also I get that it’s not an exam like you said and it’s about building your problem solving arsenal. I’d understand trying to implement a stack with 3 arrays in say a language like C where it’s difficult to do, but in JavaScript it just seems too easy. Am I supposed to attempt to do this problem without using the built in array functions? If so then that seems like more of a problem but using the built in functions it just doesn’t make sense to why I would do that.
That's a fair point. However... The difference between a good developer and a great developer is that great developers understand how things work even if they don't really need to and are able to assess multiple options and pick the right approach based on the trade-offs of each. Through these seemingly easy questions, an experimented interviewer can assess whether a candidate jumped on the most obvious solution or if they thought about different alternatives and picked the best option for the job. Very often, when implementing APIs, authors will aim for the most efficient solution on average. This is not necessarily the best option for every case though. If I asked that question in an interview and the candidate used the array API, I would ask them these two follow-up questions: - can you explain how the push/pull functions works? - do you think we can do better by reducing memory consumption / cpu time? I will most likely fail a person who struggle to come up with a reasonable justification for their solution. Sorry for the long answer but what I'm trying to say is, nothing is as simple as one may think 😉
"Do I need to know algs to alg?" Huh.
Well I can use an array as a stack. Just use the push and pop methods. I guess I’m just not understanding something you guys take for granted. tf am I missing
Stack isn’t an alg it’s a data structure anyways. The array datastracture behaves like a stack with the built in methods no?
“Implement a stack using 3 arrays.” This works in JavaScript. It seems that the built in methods of arrays make it too simple, or what am I missing? Seems like I should skip these kinds of questions no?
Be careful. Push, shift, and unshift are not always O(1) due to dynamic resizing. A linked list is guaranteed O(1) as stack or queue. You're going to need stack or queue for dfs/bfs. It's common for a JS/front end interview to ask tree/graph questions. Do not skip the chapter
Health & Wellness
7h
3356
Why are women naked in gym?
2024 Tax
6h
999
Biden’s new tax proposal is wild
Tech Industry
12h
2082
Go woke, go broke: Google fires 28 employees involved in pro-Hamas protest
Cars
Yesterday
1537
Why are Americans obsessed with SUV?
Layoffs
Yesterday
33680
Google CFO confirms "large-scale" layoffs today (Apr 17)
How else do you intend on searching unweighted graphs without using recursion
Just store the nodes in an array and shift/unshift, pop/push as needed
That’s what a stack is bruv. Abstract data type vs. Data Structure