Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

import java.util.HashMap;
import java.util.Map;

public class ProductMap {
public static void main(String []args) {
Detail l_detail1 = new Detail("A",1,"File1");
Detail l_detail2 = new Detail("B",2,"File2");

Key l_key1 = new Key(l_detail1);
Key l_key2 = new Key(l_detail2);

Map<Key,Detail> l_objMap= new HashMap<Key,Detail>();
l_objMap.put(l_key1,l_detail1);
l_objMap.put(l_key2,l_detail2);

DetailHashMap<String,Key> l_authMap= new DetailHashMap<String,Key>();
l_authMap.put(l_detail1.author,l_key1);
l_authMap.put(l_detail2.author,l_key2);

DetailHashMap<String,Key> l_bookMap= new DetailHashMap<String,Key>();
l_bookMap.put(l_detail1.book,l_key1);
l_bookMap.put(l_detail2.book,l_key2);
}
}
class DetailHashMap<A,B> {
Map <A,B> map;
DetailHashMap() {
map = new HashMap<A,B>();
}
public void put(A p_author, B p_detail) {
map.put(p_author, p_detail);
}
}
class Detail {
String author;
int sNo;
String book;

Detail(String p_author, int p_sNO,String p_book) {
author = p_author;
sNo = p_sNO;
book = p_book;
}
}
class Key {
String author;
String book;

Key(Detail p_detail) {
author = p_detail.author;
book = p_detail.book;
}
@Override
public int hashCode() {
return (author.hashCode() + book.hashCode());
}
@Override
public boolean equals(Object p_obj) {
if(this.author.equals(((Key)p_obj).author) && this.book.equals(((Key)p_obj).book)) {
return true;
}
return false;
}
}
// Detail class will be actual object(big object) and will have all required fields. Key class is for only specific fields of Detail class who involve in decision for searching. Suggestion invited.

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

Nice! A small suggestion would be to use a collection to hold Detail objects. Since it is a file that you are reading, you can expect more than two records.

- Amy Lee July 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you want to search on any of the categories like ItemNo or Authour or title etc you would need multiple maps.

1. You can create object which encapsulates everything.
2. Create multiple maps with Keys like(Title/Author/Product No) and value is a pointer to the Object.

You need not store the whole objects in the map.

- Abhi May 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you give more details. How this will work. Some psuedo code for data structures involved.

- LV June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@LV: psuedo code

class Product
{
	item#, prod#, category, title, author, etc...
}

void buildDS(File f, char delimiter)
{
	if(null == f)
		return;
	
	List<Product> products = new List<Product>();
	Dictionary<string, List<int>> index_title = new Dictionary<>(string, List<int>);
	// Add one dictionary per queriable meta-data (title, category, author)
	
	while(!f.eof)
	{
		string line = f.readLine();
		
		// Handle bad line scenarios

		// Create product instance and populate
		string[] infos = string.tokenize(line, delimiter);
		products.add(new Product(infos[0], ... infos[infos.length - 1]));
		
		index_title.add(prod.title, products.length - 1);		
		// Update all the dictionaries created
	}
}

HTH :)

- JSDUDE October 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use multidimensional indexed trees like KD-tree etc.
Or maintain bit index and make queries with bit operations.

- glebstepanov1992 May 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please give more details on this or point me to some online resource. Thanks

- LV June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the most direct way is build hash on title, on category, on author,
for example hash on title, need to store the map<title, Product Num>,

- suwei19870312 June 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;
import java.util.Map;

public class Product
{
private int itemNo;
private String productNo;
private String title;
private String author;
}

public class ProductMap
{
private Map <String, Product> allProducts = new HashMap<String,Product>();

//store author and [list of product ids]
private Map authorProductMap <String, List<String>> = new HashMap<String, List<String>>();

//store title and list of product ids
private Map titleProductMap <String, List<String>> = new HashMap<String, List<String>>();

//store category and list of product ids
private Map categoryProductMap <String, List<String>> = new HashMap<String , List<String>>();

private void addProductToMaps(Product item)
{
allProducts.add(item.getProductNo() , Product);
List<String> authorsForProduct = new ArrayList<String>();
if( authorProductMap.get(item.getAuthor()) == null)
{
authorsProductMap.add(item.getProductId());
}
else
{
authorsProductMap.get(item.getAuthor()).add(item.getProductId())
}

//repeat for other maps
}

private void queryForProduct(Product item)
{
//increment the hit count by 1 for each item found during query of maps
//item product with highest hit count is the most relevant product
Map<String , Int> productHitCount = new HashMap<String,Int>();


for (each item field query the map)
Example: Query item.getAuthor() in product map if item.getAuthor != null.
Get the list of product IDs, found and add it to a hitList

Return the hitCount of each product Id, in sorted order.
}

}

- Farooq July 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I suggest - create an object to hold each record. Place every record read into a Map where the Book id is key.
For every other search index, create a new Map with key as the property value (e.g. Title) and the value field as the id of the record.
This is similar to primary and secondary indexes seen in database. Hope this helps.

- Anonymous August 18, 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