Interview Question for Software Architects


Country: India
Interview Type: In-Person




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

Here I have assumed that any rank can be the key for searching. So, First I have created a Key abstract class and GKey, RKey CKey and FKey classes for 4 types of rank Then I have Created a person class and have used those 4 types of key as member variable. The codes are given bellow.:

public abstract class Key {

	int rank;
	public Key(int rank) {
		
		this.rank=rank;
	}
	
	public int getRank() {
		return rank;
	}
	
	@Override
	public String toString() {
		return rank+" ";
	}
}

Bellow The concrete classes of key:

public final class CKey extends Key {

	final int c_key;
	public CKey(int c_key) {
		super(c_key);
		this.c_key=c_key;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + c_key;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CKey other = (CKey) obj;
		if (c_key != other.c_key)
			return false;
		return true;
	}
	
	
	
}

In same way we shall implement CKey, GKey, FKey and RKey.

Now the person class will be like bellow:

public class Person {

	String name;
	String number;
	String address;
	
	Key g_rank;
	
	Key r_rank;
	Key c_rank;
	Key f_rank;
	
	public Person(String name, String number, String address, int g_rank, int c_rank, int r_rank, int f_rank ) {
		
		this.name=name;
		this.number=number;
		this.address=address;
		this.g_rank=new GKey(g_rank);
		this.r_rank=new RKey(r_rank);
		this.c_rank=new CKey(c_rank);
		this.f_rank=new FKey(f_rank);
	}
	
	
	public String getAddress() {
		return address;
	}
	public int getC_rank() {
		return c_rank.getRank();
	}
	public int getF_rank() {
		return f_rank.getRank();
	}
	public int getG_rank() {
		return g_rank.getRank();
	}
	public String getName() {
		return name;
	}
	public String getNumber() {
		return number;
	}
	public int getR_rank() {
		return r_rank.getRank();
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public void setC_rank(int c_rank) {
		this.c_rank = new CKey(c_rank);
	}
	public void setF_rank(int f_rank) {
		this.f_rank = new FKey(f_rank);
	}
	public void setG_rank(int g_rank) {
		this.g_rank = new GKey(g_rank);
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public void setR_rank(int r_rank) {
		this.r_rank = new RKey(r_rank);
	}
	
	
	@Override
	public String toString() {
	
		return name+" "+number+" "+address+" "+g_rank+" "+r_rank+" "+c_rank+" "+f_rank;
	}
	
	
}

Then we are having organization class that will use person class:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Organization {
	
	Map<String,Person> personList=new HashMap<String, Person>();
	
	HashMap<Key, List<String>> rankList= new HashMap<Key, List<String>>();
	

	
	
	public Organization() {
		// TODO Auto-generated constructor stub
	}
	
	
	private void insert(Key key, Person p)
	{
		if(!rankList.containsKey(key))
		{
			ArrayList<String> personNumberList= new ArrayList<String>();
			personNumberList.add(p.getNumber());
			rankList.put(key, personNumberList);
		}
		else
		{
		List<String> temp= rankList.get(key);
		temp.add(p.getNumber());
		rankList.put(key, temp);
		}
	}
	
	public void insert(Person p)
	{
		personList.put(p.getNumber(), p);
		
		Key c_key=new CKey(p.getC_rank());
		insert(c_key, p);
		
		Key f_key= new FKey(p.getF_rank());		
		
		insert(f_key, p);
		
		Key r_key= new RKey(p.getR_rank());		
		insert(r_key, p);
		
		Key g_key= new GKey(p.getG_rank());		
		insert(g_key, p);
		
	}

	public List<Person> retrieve(Key key)
	{
		System.out.println(key);
		List<String> numberList=rankList.get(key);
		System.out.println(numberList);
		List<Person> personL=new ArrayList<Person>();
		for(String number:numberList)
		{
		personL.add(personList.get(number));
		}
		return personL;
	}

//this part is to retrieve person based on key
class MyComparator implements java.util.Comparator<Person>
	{
		Class keytype;
		public MyComparator() {
			// TODO Auto-generated constructor stub
		}
		
		public MyComparator(Class keyType) {
			this.keytype=keyType;
		}
		
		@Override
		public int compare(Person o1, Person o2) {
			if(keytype == GKey.class)
			{
				return o1.getG_rank()-o2.getG_rank();
			}
			else if(keytype==CKey.class)
			{
				return o1.getC_rank()-o2.getC_rank();
			}
			else if(keytype==FKey.class)
			{
				return o1.getF_rank()-o2.getF_rank();
			}
			else
			{
				return o1.getR_rank()-o2.getR_rank();
			}
		}
	}
	public List<Person> retrievetop(Class keyType)
	{
		List<Person> personL= new ArrayList<Person>(personList.values());
	
		Collections.sort(personL,new MyComparator(keyType));
		
		return personL;
	}
}

And finally we have Test class to test the whole thing:

import java.util.List;

public class Test {

	public static void main(String args[])
	{
		Organization org= new Organization();
		
		org.insert(new Person("abcd", "a001", "abcd", 101, 101, 4, 7));
		org.insert(new Person("abcd1", "a002", "abcd1", 121, 101, 41, 71));
		org.insert(new Person("abcd2", "a003", "abcd2", 101, 102, 42, 72));
		org.insert(new Person("abcd3", "a004", "abcd3", 123, 103, 43, 73));
		org.insert(new Person("abcd4", "a005", "abcd4", 124, 104, 44, 74));
		
		
		Key key= new CKey(101);
		
		List<Person> person= org.retrieve(key);
		
		System.out.println(person);

     //this part is to get sorted list of person based on a key

List<Person>personL=org.retrievetop(GKey.class);
		System.out.println("name number address g_rank c_rank  r_rank f_rank");
		for(Person p: personL)
		{
			System.out.println(p);
		}
	}
}

- Ghosh July 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now , if I want to retrieve top 10 ranked ppl in gkey , ckey , how can I do tat?? we have rank list ...but this ranklist is based on which rank?

- gopi.komanduri July 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

To do such, I have added code above. Please check retrievetop() method for that. Infact it will return list of person sorted based on the key provided. And from there you can take top number of person easily.

- Ghosh July 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I see heap is the best solution, retrievetop(elements at max heap root), updating ranks(update elements in heap), insert persons(add elements to heap)..

- R July 20, 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