Interview Question






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

use a trie

- Anonymous May 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class Contact implements Comparable<Contact> {
	private String firstName;
	private String lastName;
	private String phoneNumber;
@Override
	public int compareTo(Contact o) {
		return getFirstName().compareTo(o.getFirstName());
	}
}

public class AddressBook {
	Set<Contact> contacts;
	public AddressBook() {
		contacts = new TreeSet<Contact>();
	}
}

- abhi1301 May 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice1.....apt use of treeset for ordered insertion and compareTo() in comparable interface method for Collections.sort() method.

- chandan prakash May 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;


class Contact implements Comparable<Contact>
{
private String firstName;
private String lastName;
private long phoneNumber;

Contact(String fname,String lname,long phno)
{
firstName=fname;
lastName=lname;
phoneNumber=phno;

}
void setFirstName()
{
this.firstName=firstName;
}
void setLastName()
{
this.lastName=lastName;
}

String getFirstName()
{
return firstName;
}
String getLastName()
{
return lastName;
}


@Override
public int compareTo(Contact o)
{
return getFirstName().compareTo(o.getFirstName());
}
}



class AddressBook
{
Set<Contact> contacts;
public AddressBook()
{
contacts= new TreeSet<Contact>();
}

public static void main(String a[])
{
AddressBook ob=new AddressBook();
contacts.add(new Contact("shashank","mani",8804567889));
contacts.add(new Contact("rahul","sharma",90045067889));
contacts.add(new Contact("mohan","mani",9894568089));
contacts.add(new Contact("rajesh","sharma",12885607889));
contacts.add(new Contact("ashutosh","mani",7834567889));
contacts.add(new Contact("raja","goyal",6734567889));
contacts.add(new Contact("mayank","sharma",2234467889));
contacts.add(new Contact("bullu","verma",1234568809));
contacts.add(new Contact("nimish","narayan",8256907834));
contacts.add(new Contact("c.p","sharm",9256738890));


Iterator it=contacts.iterator();
while(it.hasNext())
System.out.println(it.next());
}



}


Still More Needed..?

- WgpShashank May 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in java we have properties class that is widely used to implement the telephone book

One of the most useful aspects of Properties is that the information contained in a
Properties object can be easily stored to or loaded from disk with the store( ) and
load( ) methods. At any time, you can write a Properties object to a stream or read
it back. This makes property lists especially convenient for implementing simple
databases. For example, the following program uses a property list to create a simple
computerized telephone book that stores names and phone numbers. To find a
person’s number, you enter his or her name. The program uses the store( ) and load( )
methods to store and retrieve the list. When the program executes, it first tries to load
the list from a file called phonebook.dat. If this file exists, the list is loaded. You can
then add to the list. If you do, the new list is saved when you terminate the program.
Notice how little code is required to implement a small, but functional, computerized
phone book.
/* A simple telephone number database that uses a property list. */

import java.io.*;
import java.util.*;

class Phonebook
{
public static void main(String args[])throws IOException
{
Properties ht = new Properties();
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String name, number;
FileInputStream fin = null;
boolean changed = false;

// Try to open phonebook.dat file.
try
{
fin = new FileInputStream("phonebook.dat");
}
catch(FileNotFoundException e)
{
// ignore missing file
}

/* If phonebook file already exists,
load existing telephone numbers. */

try
{
if(fin != null)
{
ht.load(fin);
fin.close();
}
}
catch(IOException e)
{
System.out.println("Error reading file.");
}

// Let user enter new names and numbers.
do
{

System.out.println("Enter new name" + " ('quit' to stop): ");
name = br.readLine();
if(name.equals("quit")) continue;
System.out.println("Enter number: ");
number = br.readLine();
ht.put(name, number);
changed = true;
}
while(!name.equals("quit"));

// If phone book data has changed, save it.
if(changed)
{
FileOutputStream fout = new FileOutputStream("phonebook.dat");
ht.store(fout, "Telephone Book");

fout.close();
}

// Look up numbers given a name.
do
{

System.out.println("Enter name to find" + " ('quit' to quit): ");
name = br.readLine();
if(name.equals("quit")) continue;
number = (String) ht.get(name);
System.out.println(number);
} while(!name.equals("quit"));
}
}

- WgpShashank May 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C# we can use Sorted Dictionary.

The First name will be the key, the DS sorts itself......The value can be a class which stores the last name and phone number...we can also use tuple...maybe in .NET 3.5 and above...

- Axl September 14, 2011 | 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