Amazon Interview Question for Software Engineer / Developers






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

Hi friend,
I have a simple program which uses a link list:

LNode.java
public class LNode
{
protected Object data;
protected LNode next;
public LNode() {
next=null;
data=null;
}
public LNode(Object d,LNode n) {
data=d;
next=n;
}
public void setNext(LNode n) {
next=n;
}
public void setData(Object d) {
data=d;
}
public LNode getNext() {
return next;
}
public Object getData() {
return data;
}
public String toString() {
return " "+data;
}
}
LList.java
public class LList
{
protected LNode head;
protected int number;
public LList() {
head=null;
number=0;
}
public boolean isEmpty() {
return head==null;
}
public int size() {
return number;
}
public void add(Object o) {
head=new LNode(o,head);
number++;
}
public void append(Object o) {
if(isEmpty())
add(o);
else {
LNode t=head;
while(t.getNext()!=null)
t=t.getNext();
LNode tmp=new LNode(o,t.getNext());
t.setNext(tmp);
number++;
}
}
public Object remove()
{
if(isEmpty())
return null;
LNode tmp=head;
head=tmp.getNext();
number--;
return tmp.getData();
}
public Object removeEnd()
{
if(isEmpty())
return null;
if(head.getNext()==null)
return remove();

LNode tmp=head;
while(tmp.getNext().getNext()!=null)
tmp=tmp.getNext();
Object o=tmp.getNext().getData();
tmp.setNext(tmp.getNext().getNext());
number--;
return o;
}
public void print()
{
LNode tmp=head;
while(tmp!=null)
{
System.out.print(" "+tmp.getData());
tmp=tmp.getNext();
}
}
public Object peek(int n) {
LNode t=head;
for(int i=0;i<n && t!=null;i++)
t=t.getNext();
return t.getData();
}
}

The program is as follows:

public class NthToLast
{
public NthToLast() {
}
public static Object nthToLast(LList linkedList,int n) {
int size=linkedList.size();
return linkedList.peek(size-n);
}

public static void main(String[] args)
{
LList linkedList=new LList();
Integer o=null;
for(int i=0;i<12;i++){
o= new Integer((int)(Math.random()*100));
linkedList.add(o);
}
linkedList.print();
int n=10;
System.out.println("\n" +NthToLast.nthToLast(linkedList,n));
}
}
which checks for the 10th element from last in the single Linked list.

- rajtaresh February 21, 2008 | 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