## Amazon Interview Question for Software Engineer / Developers

• 0

Country: -
Interview Type: In-Person

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

``````const int QUEUE_SIZE = 1000;
int push_ix = 0;
int pop_ix = 0;
int num_elements = 0;
int queue[QUEUE_SIZE];
void push_queue(int val) {
if (num_elements == QUEUE_SIZE) return;
queue[push_ix] = val;
push_ix++;
if (push_ix == QUEUE_SIZE) push_ix = 0;
num_elements++;
}
int pop_queue() {
if (num_elements == 0) return -1;
int ret_val = queue[pop_ix];
pop_ix++;
if (pop_ix == QUEUE_SIZE) pop_ix = 0;
num_elements--;
}``````

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

the problem with this approach is that whenever we pop the element the capacity of the queue keeps on decreasing since the tail index is always being increased.

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

do amazon really ask such questions?

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

I was asked this one, that's why I posted.

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

We can have two variable that will hold the index of location from where to pop and from where to push into the queue. We can simulate a cirular queue in this prob.

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

Its very easy question if its a simple Queue implementation. push() and pop() should be easily doable in O(1). It gets a little tricky when its random element deletion and not the first and last element.

In that case, you can swap the element to be deleted with the first or last element and do the appropriate push or pop operation on it.

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

My question exactly. But if delete is not the same as pop, is it still a queue? Did the interviewer really insist on implementing a "traditional" queue?

Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream>
#include<queue>
using namespace std;
template <class T>
void print(const queue<Y>&);
int main()
{queue<string>q; print(q);
q.push("jean"); print(q);
q.push("stefan"); print(q);
q.pop();
q.push("paul"); print(q);
q.push("jessica"); print(q);
q.push("david"); print(q);
q.pop();}
template <class T> void print(const queue<T>&q)
{queue<T>qq=q;
cout<<"size="<<qq.size();
if(qq.empty())cout<<";the queue is empty";
else{cout<<";front="<<qq.front()<<",back="<<qq.back()<<":("<<qq.front();
qq.pop();
while(!qq.empty())
{cout<<","<<qq.front();
qq.pop();}
cout<<").";}cout<<"\n";}

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

LOL!

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

dude cut some slack..she obviously did not read the question properly happens sometimes

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.

### 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.