Amazon Interview Question for Software Engineer / Developers


Team: Tax Systems
Country: United States
Interview Type: Written Test




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

public static List<Integer> numberOfItems(String s, List<Integer> startIndices, List<Integer> endIndices) {
    int[] mem = new int[s.length()];
    int count = 0;
    for (int i = 0; i < s.length(); ++i) {
        if (s.charAt(i) == '|') {
            mem[i] = count;
        } else {
            ++count;
        }
    }
    List<Integer> ans = new ArrayList<>();
    for (int i = 0; i < startIndices.size(); ++i) {
        int start = startIndices.get(i) - 1;
        int end = endIndices.get(i) - 1;
        
        while (s.charAt(start) != '|') ++start;
        while (s.charAt(end) != '|') --end;

        ans.add(mem[end] - mem[start]);
    }
    return ans;
}

- Sahil May 26, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The ans is [2, 2] whereas correct is [2,3] as per this solution. This sol is wrong

- Namita October 03, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is wrong ^
String s = "******|*|*";

System.out.println(numberOfItems(s, List.of(1, 1), List.of(5, 6)));

- Huyvanvo94 June 21, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried and came up with this answer. I was able to get a majority of the test results to pass, however, there were a few that didn't pass because they took longer than 4 seconds. Still trying to figure out a solution that is faster than this....

public static List<Integer> calculateContainerCount(String str, List<Integer> startIndexInclusive, List<Integer> endIndexInclusive) {
		List<Integer> result = new ArrayList<>();
		int size = startIndexInclusive.size() < endIndexInclusive.size() ? startIndexInclusive.size() : endIndexInclusive.size();
		for (int i = 0; i < size; i++) {
			int count = 0;
			String subString = str.substring(startIndexInclusive.get(i), endIndexInclusive.get(i) + 1);
			int firstPipe = subString.indexOf("|");
			int lastPipe = subString.lastIndexOf("|");
			if (firstPipe >= 0 && lastPipe >= 0) {
				String subSubString = subString.substring(firstPipe, lastPipe);
				for (char c : subSubString.toCharArray()) {
					if (c == '*') {
						count++;
					}
				}
			}
			result.add(count);
		}
		return result;
	}

- Candidate July 21, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def items_in_compartments(string, startInd, endInd):
    strings = map(lambda t: string[t[0]:t[1] + 1], zip(startInd, endInd))
    return list(map(lambda s: s.strip('*').count('*'), strings))

- disb July 28, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<Integer> numberOfItems(String s, List<Integer> startIndices, List<Integer> endIndices) {
String items = "";
List<Integer> itemList = new ArrayList<>();
for(int i=0;i<Math.min(startIndices.size(), endIndices.size());i++) {
items = s.substring(startIndices.get(i)-1,endIndices.get(i));
char[] itemChars = items.toCharArray();
int pair = 0;
int compCount = 0;
int cumComCount = 0;
for(char itemChar: itemChars) {
if(itemChar == '|' && pair == 0) {
pair++;
compCount = 0;
} else if(itemChar == '|' && pair == 1) {
if(compCount>0) {
cumComCount += compCount;
}
compCount = 0;
} else if(itemChar == '*' && pair == 1) {
compCount += 1;
}
}
itemList.add(cumComCount);
}
return itemList;
}

- Anonymous August 13, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int computeItemCount(String str, int start, int end) {
boolean compStart = false;
int itemCount = 0;
int compartmentCount = 0;
for (int i = start - 1; i < end; i++) {
char c = str.charAt(i);
if( !(c == '|' || c == '*')) {
throw new IllegalArgumentException("Invalid character '" + c + "'." +
"Accepted characterd are '|' and '*'");
}
if ( c == '|' && !compStart) {
compStart = true;

}
if(compStart && c == '*') {
itemCount++;
}else if(compStart && c == '|'){
compartmentCount += itemCount;
//reset itemCount. Restart
itemCount = 0;

}

}
return compartmentCount;
}


public int[] getAllItems(String str, int[] startIndices, int[] endIndices) {
assert startIndices.length == endIndices.length;
int[] result = new int[startIndices.length];
for(int i = 0 ; i < startIndices.length; i++){
result[i] = computeItemCount(str, startIndices[i], endIndices[i]);

}
return result;

}

void testGetAllItems() {
String str = "|****|**|*|*****|**";
// Find total number of items in closed compartments in the given range for the string.
int[] startIndices = {1, 3, 5, 6};
int[] endIndices = {18, 5, 10, 11};
int[] result = getAllItems(str, startIndices, endIndices);
Arrays.stream(result).forEach(e -> System.out.printf("%d, ", e));
}

public static void main(String... args){
ItemCompartments app = new ItemCompartments();
app.testGetAllItems();
}

- Andy S October 17, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

X++++Y+++Z

- X++++Y+++Z January 13, 2022 | 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