Adobe Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
This is something I did. Tested and working fine.
using System;
using System.Net;
using System.IO;
using System.Collections;
class Program
{
// Caller (block 1)
static void Main()
{
QueueStack s = new QueueStack();
s.Push("First");
s.Push("Second");
s.Push("Third");
s.Pop();
}
}
class QueueStack
{
private Queue queue;
public QueueStack()
{
this.queue = new Queue();
}
public void Push(object something)
{
this.queue.Enqueue(something);
Console.WriteLine(something + " pushed");
}
public object Pop()
{
if(this.queue.Count > 0)
{
int totalItem = this.queue.Count - 1;
Queue newQueue = new Queue();
object poppedItem = null;
for(int i= 0;i < totalItem ; i++)
{
newQueue.Enqueue(this.queue.Dequeue());
}
poppedItem = this.queue.Dequeue();
Console.WriteLine(poppedItem + " popped");
this.queue = newQueue;
return poppedItem;
}
else
{
Console.WriteLine("The stack is empty");
return null;
}
}
}
have two queue Q1, Q2.
Push operation is normal insert at one end of Q1.
for POP, dequeue all element from Q1 and enqueue in Q2, and then dequeue the last element from Q2
using two queue will behave same as one queue. As One cant dequeue the last element from Q2.
can also do it with one que using recursion...the complication of using 2 values and 2 que deletes happens only because we don't have a function to check if the que is empty. Using such a function ie. isQueEmpty() will shrink the code to just a few lines.....
pop(int *value)
{
int val1, val2;
if(-1 == (val1 = deleteQ(gQue)) { return -1; // stack empty }
if(-1 == (val2 = deleteQ(gQue)) { *value = val1; return; }
addQ(val2);
pop(value);
addQ(val1);
return 0; // success
}
Sorry totally messed up...its more like this using recursion...
void reverseQ(int *que, int *qHead, int *qTail)
{
int val;
if(-1 == deleteQ(que, qHead, qTail, &val)) return;
reverseQ(que, qHead, qTail);
addQ(gQue, gQhead, gQTail, val);
}
pop(int *value)
{
reverseQ(gQue, gQHead, gQTail);
deleteQ(gQue, gQHead, gQTail, value));
reverseQ(gQue, gQHead, gQTail);
}
I think we can have 2 variables f ( front) and r ( rear) that points to front and rear of queue.
-Assuming basic queue operation.i .e addition of value at rear and deletion from front.
-Whenever addition of a value to queue takes place, swap f and r values after adding
-Whenever deletion of a value from queue, swap f and r values after deletion
For example, lets say we have this queue:
f-> 1, 2 <-r
now adding 3 to queue:
f-> 1,2,3 <-r
Now swap,
r-> 1,2,3 <-f
Thus next value deleted (i.e. pop) will be from last value entered.
consider we proceed to add 4 after 3, thus queue becomes:
f-> 4,1,2,3 <-r
Now if you delete from f(front) i.e. pop it will be swapping f and r :
r-> 1,2,3 <-f
Thus this acts as LIFO data structure i.e. Stack.
Time complexity of push and pop= O(1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackImplimentationUsingQueue
{
class Program
{
public class Node
{
public int data;
public Node link;
}
public class Queue
{
public Node rear;
public Node front;
public int size = 0;
public void EnQueue(int data)
{
Node n = new Node();
n.data = data;
n.link = null;
if (rear == null)
front = rear = n;
else
{
rear.link = n;
rear = n;
}
size++;
Display();
}
public Node DeQueue()
{
Node temp = new Node();
if (front == null)
Console.WriteLine("Empty");
else
{
temp = front;
front = front.link;
size--;
}
Display();
return temp;
}
public void Display()
{
if (size == 0)
Console.WriteLine("Empty");
else
{
Console.Clear();
Node n = front;
while (n != null)
{
Console.WriteLine(n.data);
n = n.link;
}
}
}
}
public class Stack
{
public Queue q;
public int size = 0;
public Node top;
public Stack()
{
q = new Queue();
}
public void Push(int data)
{
Node n = new Node();
n.data = data;
q.EnQueue(data);
size++;
int counter = size;
while (counter > 1)
{
q.EnQueue(q.DeQueue().data);
counter--;
}
}
public void Pop()
{
q.DeQueue();
size--;
}
}
static void Main(string[] args)
{
Stack s= new Stack();
for (int i = 1; i <= 3; i++)
s.Push(i);
for (int i = 1; i < 3; i++)
s.Pop();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackImplimentationUsingQueue
{
class Program
{
public class Node
{
public int data;
public Node link;
}
public class Queue
{
public Node rear;
public Node front;
public int size = 0;
public void EnQueue(int data)
{
Node n = new Node();
n.data = data;
n.link = null;
if (rear == null)
front = rear = n;
else
{
rear.link = n;
rear = n;
}
size++;
Display();
}
public Node DeQueue()
{
Node temp = new Node();
if (front == null)
Console.WriteLine("Empty");
else
{
temp = front;
front = front.link;
size--;
}
Display();
return temp;
}
public void Display()
{
if (size == 0)
Console.WriteLine("Empty");
else
{
Console.Clear();
Node n = front;
while (n != null)
{
Console.WriteLine(n.data);
n = n.link;
}
}
}
}
public class Stack
{
public Queue q;
public int size = 0;
public Node top;
public Stack()
{
q = new Queue();
}
public void Push(int data)
{
Node n = new Node();
n.data = data;
q.EnQueue(data);
size++;
int counter = size;
while (counter > 1)
{
q.EnQueue(q.DeQueue().data);
counter--;
}
}
public void Pop()
{
q.DeQueue();
size--;
}
}
static void Main(string[] args)
{
Stack s= new Stack();
for (int i = 1; i <= 3; i++)
s.Push(i);
for (int i = 1; i < 3; i++)
s.Pop();
Console.ReadKey();
}
}
}
Can be done with one queue itself.
Push :
Normal EnQueue at the end
Pop:
- jmincoder November 01, 2012