Cisco Systems Interview Question for Software Engineer / Developers






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

namespace Queue1 {
    class Program {
        static void Main(string[] args) {
            Queue myQ = new Queue(10);
            myQ.EnQueue(10);
            myQ.EnQueue(20);
            myQ.EnQueue(30);

            Console.WriteLine(myQ.DeQueue());
            Console.WriteLine(myQ.DeQueue());

            myQ.EnQueue(100);
            myQ.EnQueue(200);

            Console.WriteLine(myQ.DeQueue());

            myQ.EnQueue(300);

            Console.WriteLine(myQ.DeQueue());
            Console.WriteLine(myQ.DeQueue());
            Console.WriteLine(myQ.DeQueue());


            Console.ReadLine();
        }
    }

    class Queue {
        Stack Orig, Temp;
        int max_size;
        int curr_size = 0;
        public Queue(int sz) {
            Orig = new Stack(sz);
            Temp = new Stack(sz);
            max_size = sz;
        }

        public int DeQueue() {
            int c = curr_size;
            while (--c > 0) {
                Temp.Push(Orig.Pop());
            }
            int val = Orig.Pop();
            while (++c < curr_size) {
                Orig.Push(Temp.Pop());
            }
            curr_size--;
            return val;
        }

        public void EnQueue(int data) {
            Orig.Push(data);
            curr_size++;
        }
    }

    class Stack {
        int[] arr;
        int curr = -1;
        int size;
        public Stack(int sz) {
            if (sz < 1) throw new Exception("Size has to be greater than Zero");
            arr = new int[sz];
            size = sz;
        }

        public void Push(int data) {
            if (curr >= size-1) throw new Exception("Stack Overflow.");
            arr[++curr] = data;
        }

        public int Pop() {
            if (curr < 0) throw new Exception("Stack Underflow.");
            return arr[curr--];
        }
    }
}

- sk May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use two stack to implement the queue.

Add function of queue:-
push item in stack1 but be sure that stack2 is empty.
if stack 2 is not empty then pop items from stack2 and push it in stack1
now add in stack1.

Delete function of queue:-
While deletion make sure that stack1 is empty.
If stack1 is not empty then pop items from stack1 and push it in stack2.
now pop item from stack2


void delete(int *x , stack_type *st_ptr1 , stack_type *st_ptr2)
{
int item;
while(!(stack_empty(st_ptr1)))
{
pop(&item,st_ptr1);
push(item,st_ptr2);
}
pop(x,st_ptr2);
}
void add(int x,stack_type *st_ptr1 , stack_type *st_ptr2)
{
int item;
while(!(stack_empty(st_ptr2)))
{
pop(&item,st_ptr2);
push(item,st_ptr1);
}
push(x,st_ptr1);
}


for complete c code
see "goursaha.freeoda.com/DataStructure/QueueUsing2Stack.html"

- Anonymous May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use two stacks:
Stack1 = an LIFO stack that we will use to implement FIFO behavior
Stack2 = an LIFO stack to help us re-arrange the items in Stack1

Pushing an item X in Stack1:
1) If Stack1 is empty, push the item.
2) If Stack1 is not empty:
a) pop all items in Stack1 and push then in Stack2 one at a time
b) push the item X in Stack1
c) pop all items in Stack2 and push them in Stack1 one at a time

Popping an item in Stack1:
1) The items in the Stack1 are now arranged such that the first item pushed in Stack1 is now the first item to be popped.

- Anonymous August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

LIFO is the correct acronym, not FILO :) just saying

- nomi October 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stack in, out;
void enqueue(int value)
{
while(!out.empty())
{
in.push(out.pop());
}
in.push(value);
}
int dequeue()
{
while(!in.empty())
{
out.push(in.pop());
}
return out.pop();
}

- lliyanc April 11, 2012 | 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