Amdocs Interview Question


Country: United States




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

public class Book {

String title;
String author;
int count;

public Book()
{
title="";
author="";
count=0;
}

public Book(String title,String author)
{
this.title=title;
this.author=author;
count=1;
}

public Book(String title,String author,int count)
{
this.title=title;
this.author=author;
this.count=count;
}

public void increment()
{
count++;
}

public void decrement()
{
if(count>0)
count--;
}
}



public class Library {

Book[] books;

public Library()
{
books=new Book[0];
}

public Library(Book[] books)
{
this.books=books;
}

public int getCount(String title)
{
//given the title of a book as parameter, return how many copies of the book are there in the library
int count=0;
for(int i=0;i<books.length;i++)
{
if(books[i].title.equalsIgnoreCase(title));
{
count= books[i].count;
break;
}
}
return count;

}

public int getCount(Book book)
{
//given the object of type Book as parameter, return how many copies are there of that book in the library
int count=0;
for(int i=0;i<books.length;i++)
{
if(books[i].title.equalsIgnoreCase(book.title)&&(books[i].author.equalsIgnoreCase(book.author)));
{
count= books[i].count;
break;
}
}
return count;
}

public int getCount()
{
//returns the total number of books in the library
int count=0;
for(int i=0;i<books.length;i++)
{
count+=books[i].count;
}
return count;
}

public int getDiffCount()
{
//returns the number of different books in the library
return books.length;
}

public void addBook(Book book)
{
//given an object of type Book as input, increment its count if it is in the library else adds the book.

int pos=-1;
for(int i=0;i<books.length;i++)
{
if(books[i].title.equalsIgnoreCase(book.title)&&(books[i].author.equalsIgnoreCase(book.author)))
{
pos=i;
books[i].count+=book.count;
break;
}
}
if(pos==-1)
{
Book[] newbooks=new Book[books.length+1];
int i;
for(i=0;i<books.length;i++)
{
newbooks[i]=books[i];
}
newbooks[i]=book;
books=newbooks;
}
}

public void removeBook(Book book)
{
boolean flag=false;
int i;
for(i=0;i<books.length;i++)
{
if(books[i].title.equalsIgnoreCase(book.title)&&(books[i].author.equalsIgnoreCase(book.author)))
{
books[i].count-=book.count;
if(books[i].count<=0)
flag=true;
break;
}
}
if(flag)
{
Book[] newbooks=new Book[books.length-1];
for(int j=0,k=0;k<newbooks.length;j++,k++)
{
if(j==i)
j++;
newbooks[k]=books[j];
}
books=newbooks;
}
}
}

- Anonymous November 01, 2014 | 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