From aa66d9b83d5793bdbb7fe28368e0642f7c3dceac Mon Sep 17 00:00:00 2001 From: William Carroll Date: Thu, 12 Nov 2020 14:37:29 +0000 Subject: Add coding exercises for Facebook interviews Add attempts at solving coding problems to Briefcase. --- scratch/facebook/queue-two-stacks.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 scratch/facebook/queue-two-stacks.py (limited to 'scratch/facebook/queue-two-stacks.py') diff --git a/scratch/facebook/queue-two-stacks.py b/scratch/facebook/queue-two-stacks.py new file mode 100644 index 000000000000..a71abeb00552 --- /dev/null +++ b/scratch/facebook/queue-two-stacks.py @@ -0,0 +1,20 @@ +from stack import Stack + +class Queue(object): + def __init__(self): + self.lhs = Stack() + self.rhs = Stack() + + def enqueue(self, x): + self.rhs.push(x) + + def dequeue(self, x): + y = self.rhs.pop() + while y: + self.lhs.push(y) + y = self.rhs.pop() + result = self.lhs.pop() + y = self.lhs.pop() + while y: + self.rhs.push(y) + return result -- cgit 1.4.1