Google Interview Question: Implement A Queue With A Stack - Whiteboard Wednesday

  Рет қаралды 55,323

Irfan Baqui

Irfan Baqui

Күн бұрын

Check out the detailed data structures and algorithms course at www.interviewa...
Welcome to Whiteboard Wednesday where I do software engineering interview problems on the whiteboard in a real interview setting.
Today's problem is asked at Google as well as other big companies. It's about implementing a queue with a stack.
Leave a comment on which company or problem you want us to cover next!
New video every Wednesday. Please like, subscribe and share!

Пікірлер: 52
@bjhghjkjgj
@bjhghjkjgj 6 жыл бұрын
The recursive solution is fundamentally the same as the first one, it essentially uses the callstack as the dequeue stack and puts everything back into the enqueue stack after each dequeue call.
@PrashantNigam
@PrashantNigam 6 жыл бұрын
Exactly. Iteration is the way to go
@dephc0n1
@dephc0n1 6 жыл бұрын
Agreed. I wonder if you told this to the interviewer they would request you try a different approach.
@guitarockdude
@guitarockdude 6 жыл бұрын
I think she just asked because he told her he had already encountered this problem before. Sometimes interviewers want to see how you solve problems, rather than what problems you have solved.
@rajattandonmit
@rajattandonmit 5 жыл бұрын
Agree
@jgarcia2013
@jgarcia2013 4 жыл бұрын
yep, it's still a two stacks solution.
@moocow20391
@moocow20391 6 жыл бұрын
the recursive solution for mimicking queue with one stack is inefficient though. As someone already said, it implicitly uses callstack as deque stack and additionally there is lots of function call overhead involved. So don't be fooled into thinking that it is somehow more elegant version than the first one with two stacks!
@CompleteMeYT
@CompleteMeYT 5 жыл бұрын
^ this. The 1-stack-solution is O(n) while the 2-stack solution pops in O(1) on average.
@ArvindSingh-ug7pb
@ArvindSingh-ug7pb 6 жыл бұрын
I think by using recursion you are technically using one more Stack
@sarthaksrivastav3408
@sarthaksrivastav3408 5 жыл бұрын
Yes, but in most of the techniques in which we convert a recursive approach to iterative approach we use a stack. e.g, code for dfs with iterative approach uses a stack
@byan6741
@byan6741 4 жыл бұрын
Much less efficient since the stack frames for each call are much bigger than a single integer.
@KaranSingh-ge7wz
@KaranSingh-ge7wz 5 жыл бұрын
love this concept of explaining to someone(posing as an interviewer) clarity - top level
@rahulroy9785
@rahulroy9785 5 жыл бұрын
ya
@_ityadi
@_ityadi 3 жыл бұрын
Thanks for showing we can cheat using a recursion call stack instead. I would have assumed this can not be done since I thought using recursion is basically using a stack and discarded this solution. Next time I would not assume things in the interview, rather just ask the interviewers if I can do it or not.
@harishjain7583
@harishjain7583 4 жыл бұрын
In java at least it uses stack memory instead of heap memory. That is the difference here. Using heap is costly. Stack memory will be cleared as soon as the method execution gets done or the recursion gets done.
@chandrakantkumarchoudhary1144
@chandrakantkumarchoudhary1144 3 жыл бұрын
this concept is not about which is better its about how well you understand stack and recursion'
@FistroMan
@FistroMan 6 жыл бұрын
To make recursion you need another stack of callings...
@ravineshkumar5680
@ravineshkumar5680 4 жыл бұрын
recurse() is recursive method and require a stack in memory.so,overall two stack is used.
@FranticRock
@FranticRock 5 жыл бұрын
With recursion, stack overflow in java will occur roughly after 7700 calls. So the recursive solution will not work for any queue over that in size. It should be one of the up-front questions - does this thing need to process millions of records? Recursion itself has overhead as well: Recursion takes a lot of stack space, as each time a method calls itself, a pointer to it and its local variables are generated again.
@johncanessa2250
@johncanessa2250 6 жыл бұрын
Notwithstanding performance issues with the recursive method; the class for the single stack is quite nice. Recursion seems to be elegant when the number of elements processed is relatively small; otherwise crashes and slow performance seem to creep when less expected.
@AshishGupta-rh3jf
@AshishGupta-rh3jf 6 жыл бұрын
import java.util.Stack; public class StackToQueue { public static void main(String[] args) { Stack stack = new Stack(); stack.push(10); stack.push(11); stack.push(12); stack.push(13); stack.push(14); System.out.println(stack.pop()); System.out.println(getValue(stack)); } public static T getValue(Stack stack){ if(stack.size() == 1){ return stack.pop(); } T currentValue = stack.pop(); T returnResult = getValue(stack); stack.push(currentValue); return returnResult; } }
@kyabaatsharmaji
@kyabaatsharmaji 5 жыл бұрын
your channel is really helpful! thank for you the great wok! Could you please cover the median of two integer arrays?
@debdutsaha4316
@debdutsaha4316 3 жыл бұрын
I am also thinking the same
@abhi4unme2002
@abhi4unme2002 6 жыл бұрын
Without stack ~ Call Stack :...oops More Expensive !!!
@abhishekbapat5155
@abhishekbapat5155 3 жыл бұрын
Came in the comments section to explain how recursion internally just uses another stack and how both the solutions are just the same. But turns out many people have realised that. 😂
@Christopher_126
@Christopher_126 4 жыл бұрын
You gotta work on your hand writing bro. It's ruining the video
@tirthjayswal9895
@tirthjayswal9895 4 жыл бұрын
Stack using double link list may work by keeping the pointer to bottom element
@martinbennett9908
@martinbennett9908 3 жыл бұрын
Then it's a doubly linked list and not a stack.
@_anaskhan
@_anaskhan 3 жыл бұрын
@@martinbennett9908 hahahah
@sulabh_singh
@sulabh_singh 6 жыл бұрын
I didn't get the logic of return statement. if you are doing return at (stack.lenght()==1) and before end of method. It will always return first value ,I guess.
@TheBalkanFury
@TheBalkanFury 6 жыл бұрын
The return statement is return stack.pop(). So as it returns it pops the last value making the stack be empty and on the way back enqueues all of the ones stored in the callstack. The last value (at length ==1) gets popped and never pushed back like the other values. In the end the size will be original size - 1. Hope that helps!
@vaibhavsachdeva2261
@vaibhavsachdeva2261 3 жыл бұрын
very nice sir well explained!
@rahulbhati3079
@rahulbhati3079 4 жыл бұрын
It was really very good video
@uladzimiryankouski6464
@uladzimiryankouski6464 4 жыл бұрын
i think this is much more easily and use only one stack: if you input "result" it returns whole stack, if you input "q" it returns the first item from stack, if you input anything else it appends to the top: a = input() stack = [] while a is not "result": if a is not "q": stack.append(a) else: if len(stack) > 0: print(stack[0]) del stack[0] else: print("stack is empty") a = input() print(stack)
@dhruvie
@dhruvie 4 жыл бұрын
it will work but it is using array / list and we have to implement queue with stack only , please correct me If I am wrong
@bauyrzhanmaksot3022
@bauyrzhanmaksot3022 5 жыл бұрын
interesting problem, good job
@hannarivera6158
@hannarivera6158 6 жыл бұрын
Awesome
@YogeshPersonalChannel
@YogeshPersonalChannel 5 жыл бұрын
Those mhm and ok are really not needed
@stephanbranczyk8306
@stephanbranczyk8306 3 жыл бұрын
Those are not be needed, but it's a hard habit to break for most people.
@AdityaSingh-vp1mk
@AdityaSingh-vp1mk 6 жыл бұрын
That was smart 😱😱
@andykjm
@andykjm 5 жыл бұрын
2 pointers on an array that loops on itself should do the trick!
@AmitTankprogrammer
@AmitTankprogrammer 5 жыл бұрын
If you're using 2 pointers on an array (eg. head, tail) then basically you're implementing queue using queue. Since the question clearly says to implement using stack.
@preston7376
@preston7376 6 жыл бұрын
Easier solution: use a stack of size 1, and it's a list of those numbers and never gets popped. Who said what type of stack it has to be?
@kazakhification
@kazakhification 4 жыл бұрын
Put off this music. Google interview It's not drama
@diegoc3749
@diegoc3749 6 жыл бұрын
For dequeue() I would use this approach: 1) if stack is empty, return; 2) set variable N = Stack[0]; 3) iterate through the stack until Stack.length -1, for every iteration, set the current index to the next value (Stack[i] = Stack[ i + 1]); 4) get rid of the last item: Stack.pop() 5) return N (the removed item) . This approach would be a lot faster and not use recursion.
@JoelPinto7
@JoelPinto7 6 жыл бұрын
A pure stack data structure doesn't support random access. It supports only accessing the top element. Here, he happens to use an array to implement it. With another underlying data structure like a linked list, this solution will not work.
@dhananjayaperera5880
@dhananjayaperera5880 5 жыл бұрын
This recusive () funtion is not going to end. Is it better to use recursive() within the else statement.
@Donclion911
@Donclion911 5 жыл бұрын
ur writing looks like ....... Arabic
@DaveyGuitar
@DaveyGuitar 3 жыл бұрын
I've been enjoying these problems, up until this one, because they were good practice and I see the application. This one, implementing a queue as a stack, with only one stack, makes no sense to me in logic (knowing the true purpose of a queue and stack). The user of these appliances is just supposed to be pushing items on, or popping items off....not being worried about the internals, such as length (because in something operating at 100 transactions per second, who is tracking the length at any given millisecond). The problem that is being presented by the interview lady, makes no sense to me. This one is very misleading, and not useful.
@rafaelcupello2549
@rafaelcupello2549 7 жыл бұрын
New one today?
@IrfanBaqui
@IrfanBaqui 7 жыл бұрын
Yup, here it is: kzbin.info/www/bejne/annJdaWfqKxmg7M
@z08840
@z08840 3 жыл бұрын
so, if stack is a million elements deep we are just using a million deep call STACK.... rrrrrrrrright.... well, thank you, I'm leaving - have no interest to work here :P
Do you choose Inside Out 2 or The Amazing World of Gumball? 🤔
00:19
когда не обедаешь в школе // EVA mash
00:57
EVA mash
Рет қаралды 3,8 МЛН
Players vs Corner Flags 🤯
00:28
LE FOOT EN VIDÉO
Рет қаралды 80 МЛН
Find the intersection between arrays: Coding Interview Question
11:26
Whiteboard Coding Interviews: 6 Steps to Solve Any Problem
15:18
Fullstack Academy
Рет қаралды 371 М.
Facebook Interview: K Most Frequent Elements - Whiteboard Thursday
14:24
Medium Google Coding Interview With Ben Awad
51:27
Clément Mihailescu
Рет қаралды 1,3 МЛН
How to: Work at Google - Example Coding/Engineering Interview
24:02
Life at Google
Рет қаралды 7 МЛН
Ex-Google Recruiter Reveals 8 Secrets Recruiters Won’t Tell You
13:57
Do you choose Inside Out 2 or The Amazing World of Gumball? 🤔
00:19