Amazon Interview Question
SDE1sCountry: -
Interview Type: Written Test
public class Datastructures {
public HashMap<String, Stats> stats = new HashMap<>();
public static void main(String[] args) {
Datastructures ds = new Datastructures();
try (BufferedReader reader = new BufferedReader(new FileReader("C:/Users/Gangadhar/Desktop/FleetState.txt"))) {
String line = null;
while ((line = reader.readLine()) != null) {
String[] arr = line.split(",");
Stats type = ds.stats.get(arr[1]);
if (type == null) {
type = new Stats();
ds.stats.put(arr[1], type);
}
int count = Integer.parseInt(arr[2]);
int empty = 0;
for (int i = 3; i < arr.length; i++) {
if (Integer.parseInt(arr[i]) == 0) {
empty++;
}
}
if (empty == count) {
type.empty++;
} else if (empty == 0) {
type.full++;
} else {
type.almost.hosts++;
type.almost.count += empty;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File("C:/Users/Gangadhar/Desktop/Statistics.txt")))) {
StringBuilder empty = new StringBuilder();
StringBuilder full = new StringBuilder();
StringBuilder almost = new StringBuilder();
empty.append("EMPTY:");
full.append("FULL:");
almost.append("MOST FILLED:");
for (Map.Entry<String, Stats> entry : ds.stats.entrySet()) {
Stats val = entry.getValue();
empty.append(entry.getKey()).append("=").append(val.empty).append(";");
full.append(entry.getKey()).append("=").append(val.full).append(";");
almost.append(entry.getKey()).append("=").append(val.almost.hosts).append(",").append(val.almost.count).append(";");
}
empty.append("\n");
full.append("\n");
almost.append("\n");
writer.write(empty.toString());
writer.write(full.toString());
writer.write(almost.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static class Stats {
public int empty;
public int full;
public Count almost = new Count();
}
public static class Count {
int hosts;
int count;
}
}
Okay, that is a great answer.
But what if I did the same thing in the previous answer(answer #2) which is count and hash, but with more code design like OOP & OOD Ex :(polymorphism, Single Stone ....) and with class just to valdiate all inputs, like it is areally id, it is avalid host type etc..., and the code was written in java with fantastic test class that generate all the normal and the extreme cases, on top of that I wrote some ideas on how to make the code faster by dividing the input file and use multi threading to read it and how to maintain every thing right since we are using threads now.
After that, do you reject me?, and sends me an email with new header that doesn't belong to the previous conversation and filled with wrong details, BTW I got the question on fri Oct 17 I solved it and send it back on sun Oct 19 and the recruiter said we will get to you in 2-3 days, until fri Oct 31 and after three follow up emails she answered with wrong details.
write by python.
# -*- coding: utf-8 -*-
__author__ = 'ouyhui'
class HostCalculate:
def calculate(self, host_file, host_stat_file):
empty_stat = [0 for i in xrange(3)]
full_stat = [0 for i in xrange(3)]
filled_stat = [0 for i in xrange(6)]
file = open(host_file)
for line in file:
split_index = line.index(',', line.index('M')+3)
host_info = line[:split_index].split(',')
slot_stats = line[split_index+1:]
empty_count = slot_stats.count('0')
slot_count = int(host_info[2])
host_type_index = int(host_info[1][1]) - 1
if empty_count == 0:
full_stat[host_type_index] = full_stat[host_type_index]+ 1
elif empty_count == slot_count:
empty_stat[host_type_index] = empty_stat[host_type_index]+ 1
else:
filled_stat[host_type_index * 2] = filled_stat[host_type_index * 2] + 1
filled_stat[host_type_index * 2 + 1] = filled_stat[host_type_index * 2 + 1] + empty_count
stat_file = open(host_stat_file, 'w')
stat_file.write('EMPTY: M1=%d;M2=%d;M3=%d;\n' % tuple(empty_stat))
stat_file.write('FULL: M1=%d;M2=%d;M3=%d;\n' % tuple(full_stat))
stat_file.write('MOST FILLED: M1=%d,%d;M2=%d,%d;M3=%d,%d;\n' % tuple(filled_stat))
stat_file.close()
Maintain 4 integer arrays of size 3 (type of instances - M1,M2,M3). The arrays will be used to store:
- Hacker October 31, 20141. Num of hosts with all empty slots
2. Num of hosts with all full slots
3. Min num of empty slots
4. Num of hosts with the min empty slots (corresponding to 3).
Process each line of the file and fill/update the arrays.
Print out the arrays in the expected output format.