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


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