Apple Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Create a max heap of size 5. Read the elements from the file and put it in to heap. finally u will be left with max 5 values
Public class StaticM {
public static List<File> filesInDirectory(String sDirectory){
List<File> listFiles = new ArrayList<>();
File file = new File(sDirectory);
for(File file1: Arrays.asList(file.listFiles())){
if(file1.isDirectory()){
filesInDirectory(file1.toString());
}
else{
listFiles.add(file1);
}
}
return listFiles;
}
public static Map<String, Integer> FindMostExpCust() throws IOException{
MultiValueMap custExp = new MultiValueMap();
// keep all your files in temp directory
for(File file:StaticM.filesInDirectory("C:\\temp")){
FileReader reader = new FileReader(file);
BufferedReader bufRead = new BufferedReader(reader);
String line = "";
while((line=bufRead.readLine())!=null){
// CustomerID=12345, ProductID=456, Expenses=3200
custExp.put(line.split(",")[0].split("=")[1], Integer.parseInt(line.split(",")[2].split("=")[1]));
}
}
Map<String, Integer> mp1 = new HashMap<>();
int value=0;
Iterator<?> it = custExp.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
for(int i=0;i<custExp.getCollection(key).toArray().length;i++){
value+=(int)custExp.getCollection(key).toArray()[i];
}
mp1.put(key, value);
}
return mp1;
}
public static void SortMap(Map<String,Integer> map){
List<?> list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator<Object>(){
public int compare(Object o1, Object o2){
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
System.out.println(sortedHashMap);
}
public static void main(String[]args) throws IOException{
StaticM.SortMap(StaticM.FindMostExpCust());
}
}
public class StaticM {
public static List<File> filesInDirectory(String sDirectory){
List<File> listFiles = new ArrayList<>();
File file = new File(sDirectory);
for(File file1: Arrays.asList(file.listFiles())){
if(file1.isDirectory()){
filesInDirectory(file1.toString());
}
else{
listFiles.add(file1);
}
}
return listFiles;
}
public static Map<String, Integer> FindMostExpCust() throws IOException{
MultiValueMap custExp = new MultiValueMap();
// keep all your files in temp directory
for(File file:StaticM.filesInDirectory("C:\\temp")){
FileReader reader = new FileReader(file);
BufferedReader bufRead = new BufferedReader(reader);
String line = "";
while((line=bufRead.readLine())!=null){
// CustomerID=12345, ProductID=456, Expenses=3200
custExp.put(line.split(",")[0].split("=")[1], Integer.parseInt(line.split(",")[2].split("=")[1]));
}
}
Map<String, Integer> mp1 = new HashMap<>();
int value=0;
Iterator<?> it = custExp.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
for(int i=0;i<custExp.getCollection(key).toArray().length;i++){
value+=(int)custExp.getCollection(key).toArray()[i];
}
mp1.put(key, value);
}
return mp1;
}
public static void SortMap(Map<String,Integer> map){
List<?> list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator<Object>(){
public int compare(Object o1, Object o2){
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
System.out.println(sortedHashMap);
}
public static void main(String[]args) throws IOException{
StaticM.SortMap(StaticM.FindMostExpCust());
}
}
public class StaticM {
public static List<File> filesInDirectory(String sDirectory){
List<File> listFiles = new ArrayList<>();
File file = new File(sDirectory);
for(File file1: Arrays.asList(file.listFiles())){
if(file1.isDirectory()){
filesInDirectory(file1.toString());
}
else{
listFiles.add(file1);
}
}
return listFiles;
}
public static Map<String, Integer> FindMostExpCust() throws IOException{
MultiValueMap custExp = new MultiValueMap();
// keep all your files in temp directory
for(File file:StaticM.filesInDirectory("C:\\temp")){
FileReader reader = new FileReader(file);
BufferedReader bufRead = new BufferedReader(reader);
String line = "";
while((line=bufRead.readLine())!=null){
// CustomerID=12345, ProductID=456, Expenses=3200
custExp.put(line.split(",")[0].split("=")[1], Integer.parseInt(line.split(",")[2].split("=")[1]));
}
}
Map<String, Integer> mp1 = new HashMap<>();
int value=0;
Iterator<?> it = custExp.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
for(int i=0;i<custExp.getCollection(key).toArray().length;i++){
value+=(int)custExp.getCollection(key).toArray()[i];
}
mp1.put(key, value);
}
return mp1;
}
public static void SortMap(Map<String,Integer> map){
List<?> list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator<Object>(){
public int compare(Object o1, Object o2){
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
System.out.println(sortedHashMap);
}
public static void main(String[]args) throws IOException{
StaticM.SortMap(StaticM.FindMostExpCust());
}
}
- Anonymous September 01, 2014