## Amazon Interview Question for Software Engineers

Country: United States
Interview Type: In-Person

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

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!

public boolean validForSharing(List<SharedParagraph> sharedParagraphs, Book book) {

if(sharedParagraphs.isEmpty()) return true;

//sort sharedParagraphs
Collections.sort(sharedParagraphs, new Comparator<SharedParagraph>(){
public int compare(SharedParagraph i1, SharedParagraph i2){
if(i1.start!=i2.start)
return i1.start-i2.start;
else
return i1.end-i2.end;
}
});

SharedParagraph pre = sharedParagraphs.get(0);
int totalShared = 0, start = 0, end = 0;

for(int i=0; i<sharedParagraph.size(); i++){

SharedParagraph curr = sharedParagraphs.get(i);

if(curr.start >= end) {

totalShared += end - start;
start = curr.start;
end = curr.end;
} else {

end = Math.max(curr.end, end);
}
}
totalShared += end - start;

return result <= book.size() / 10;
}

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

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.

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

public boolean selectText(String inputText, int startIndex, int endIndex){
String selectedtext = inputText.substring(startIndex,endIndex);

if(selectedtext.length() <= 0.1*inputText.length()){
return true;
}
return false;
}

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

public boolean selectText(String inputText, int startIndex, int endIndex){
String selectedtext = inputText.substring(startIndex,endIndex);

if(selectedtext.length() <= 0.1*inputText.length()){
return true;
}
return false;
}

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.

### 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.