Amazon Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
So, This is what, I would do.
Let's define an abstract entity for identifications, and storing the unique key in the database.
public abstract class Entity
{
public Guid UserId;
public Guid bookId;
}
public class SharableBooks : Entity
{
private IntervalTree sharableParts;
public double allowedShareablePart = 9876.45; // 10% of a given book
public SharableBooks(bookId, userId): base(userid, bookid)
{
sharableParts = new IntervalTree();
}
public bool Read(int startpnum, int startpindex, int endpnum, int endpindex)
{
if(sharableParts.GetTotalSharableCountAfterInsert(startpnum, startpindex, endpnum, endpindex) > allowedShareablePart)
{
return false;
}
sharableParts.Insert(startpnum, startpindex, endpnum, endpindex);
return true;
}
}
public class IntervalTree
{
private Node root;
private double sharedPageCount;
public IntervalTree()
{
root = null;
sharedPageCount = 0.0;
}
public double SharedPageCounts()
{
return sharedPageCount;
}
public double GetTotalSharableCountAfterInsert(int startpnum, int startpindex, int endpnum, int endpindex)
{
return sharedPageCount + Insert(startpnum, startpindex, endpnum, endpindex, false);
}
public bool Insert(int startpnum, int startpindex, int endpnum, int endpindex)
{
sharedPageCount += Insert(startpnum, startpindex, endpnum, endpindex, true);
return true;
}
public double Insert(int startpnum, int startpindex, int endpnum, int endpindex, bool isInsert)
{
if(root == null)
{
if(isInsert)
{
root = new Node(startpnum, startpindex, endpnum, endpindex);
}
return CountPage(startpnum, startpindex, endpnum, endpindex);
}
Node newNode = root;
return InsertInternal(newNode, startpnum, startpindex, endpnum, endpindex, isInsert);
}
private double InsertInternal(Node node, int startpnum, int startpindex, int endpnum, int endpindex, bool isInsert)
{
Node newNode = node;
Node parentNode = newNode;
while(newNode != null)
{
parentNode = newNode;
if(endpnum < newNode.startPageNum || (endpnum == newNode.startPageNum && endpindex < newNode.startPageIndex))
{
newNode = newNode.left;
}
else if(startpnum > newNode.endPageNum || (startpnum == newNode.endPageNum && startpindex > newNode.endPageIndex))
{
newNode = newNode.right;
}
else
{
break;
}
}
if(newNode == null)
{
if(endpnum < parentNode.startPageNum || (endpnum == parentNode.startPageNum && endpindex <= parentNode.startPageIndex))
{
if(isInsert)
{
parentNode.left = new Node(startpnum, startpindex, endpnum, endpindex);
}
return CountPage(startpnum, startpindex, endpnum, endpindex);
}
else if(startpnum > parentNode.endPageNum || (startpnum == parentNode.endPageNum && startpindex >= parentNode.endPageIndex))
{
if(isInsert)
{
parentNode.right = new Node(startpnum, startpindex, endpnum, endpindex);
}
return CountPage(startpnum, startpindex, endpnum, endpindex);
}
}
else
{
if(ContainedInCurrentNode(newNode, startpnum, startpindex, endpnum, endpindex))
{
return 0.0;
}
else
{
if((startpnum > newNode.startPageNum && startpnum < newNode.endPageNum) ||
(startpnum == newNode.startPageNum && startpindex > newNode.startPageIndex) ||
(startpnum == newNode.endPageNum && startpindex < newNode.endPageIndex))
{
if(newNode.right == null)
{
if(isInsert)
{
newNode.right = new Node(newNode.endPageNum, newNode.endPageIndex, endpnum, endpindex);
}
return CountPage(newNode.endPageNum, newNode.endPageIndex, endpnum, endpindex);
}
return InsertInternal(newNode.right, newNode.endPageNum, newNode.endPageIndex, endpnum, endpindex, isInsert);
}
else
{
if(newNode.left == null)
{
if(isInsert)
{
newNode.left = new Node(startpnum, startpindex, newNode.startPageNum, newNode.startPageIndex);
}
return CountPage(startpnum, startpindex, newNode.startPageNum, newNode.startPageIndex);
}
return InsertInternal(newNode.left, startpnum, startpindex, newNode.startPageNum, newNode.startPageIndex, isInsert);
}
}
}
}
private bool ContainedInCurrentNode(Node node, int startpnum, int startpindex, int endpnum, int endpindex)
{
return ((node.startPageNum < startpnum || (node.startPageNum == startpnum && node.startPageIndex <= startpindex)) &&
(node.endPageNum > endpnum || (node.endPageNum == endpnum && node.endPageIndex >= endpindex)));
}
public static double CountPage(int startpnum, int startpindex, int endpnum, int endpindex)
{
int count = 0;
if(endpnum > startpnum+1)
{
count += endpnum - startpnum;
startpnum = endpnum - 1;
}
if(endpnum = startpnum + 1 && startpindex <= endpindex)
{
count += 1.0;
startpnum = endpnum;
}
if(startpnum == endpnum)
{
count += ((double)endpindex - (double)startpindex)/Math.Max(endpindex - startpindex, 1000);
}
else if(endpnum = startpnum + 1)
{
count += (Math.Max(startpindex - endpindex, 1000.0) - ((double)startpindex - (double)endpindex))/Math.Max(startpindex - endpindex, 1000);
}
return count;
}
}
public class Node
{
public int startPageNum;
public int startPageIndex;
public int endPageNum;
public int endPageIndex;
public Node left;
public Node right;
public Node(int startpnum, int startpindex, int endpnum, int endpindex)
{
startPageNum = startpnum;
startPageIndex = startpindex;
endPageNum = endpnum;
endPageIndex = endpindex;
left = null;
right = null;
}
}
Complexity of this O(height) of interval tree for any search and read operation.
Looking for interview experience sharing and mentors?
Visit A++ Coding Bootcamp at aonecode.com.
Taught by experienced engineers/interviewers from FB, Google and Uber,
our ONE TO ONE courses cover everything in an interview including
latest interview questions sorted by companies,
SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)
ALGORITHMS (conquer DP, Graph, Greedy and other advanced algo problems),
and mock interviews.
Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.
Welcome to email us aonecoding@gmail.com with any questions. Thanks!
- aonecoding April 23, 2017