Adobe Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
6
of 6 vote

Can be done with one queue itself.
Push :
Normal EnQueue at the end
Pop:

count = q.Size();
  if count == 0 return "nothing to pop"
  DeQeuue count-1 elements and Enqueue back to same queue
  DeQueue and return.

- jmincoder November 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution.

- pradegup November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check my answer to see how I implemented the same logic using C#.

- Tarik January 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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;
    }
  }
}

- Tarik January 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

using two queue will behave same as one queue. As One cant dequeue the last element from Q2.

- ronak November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can we reverse Q1 when popping and then dequeue and then again reverse the queue. This way we do not need 2nd queue

- DashDash March 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
}

- MJ November 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);
}

- MJ November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have two Qs : q1, q2 .

Push : After first insertion, Push in the queue which is not empty.
- Check if q1 is empty , then insert in q2.
- Else insert in q1
O(1)
Pop : Transfer n-1 elements from non-empty Q to the empty, and fetch the nth element from the non-empty Q

- Prashant November 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pop() complexity : O(n)

- Prashant November 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- Kartik March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
        }
    }
}

- Anonymous November 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 c# with single Queue November 15, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More