Skip to content

Instantly share code, notes, and snippets.

@gomathi
Created March 26, 2015 16:52
Show Gist options
  • Save gomathi/827b446be7d0d4611d03 to your computer and use it in GitHub Desktop.
Save gomathi/827b446be7d0d4611d03 to your computer and use it in GitHub Desktop.
CodingExercise
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) throws FileNotFoundException,
IOException {
String fPath = "/Users/gomes/Downloads/gistc86063404fd122786480-b88daddef45437343cbc99c8729df6e1fd95b415/input2_loadtimes.txt";
List<VisitedLogData> vLogDataList = VisitedPagesParser
.parseVisitedLogData(VisitedPagesParser.parseRawData(fPath));
Collection<List<String>> grouped = VisitedPagesParser
.groupPathsByCustomers(vLogDataList);
Map<Path, AtomicInteger> groupedCount = VisitedPagesParser
.groupPathByVisitedCount(grouped);
System.out.println(VisitedPagesParser
.findMaximumVisitedPath(groupedCount));
}
}
class VisitedPagesParser {
public static List<VisitedRawData> parseRawData(String fileName)
throws FileNotFoundException, IOException {
List<VisitedRawData> result = new ArrayList<VisitedRawData>();
try (BufferedReader br = new BufferedReader(new FileReader(new File(
fileName)))) {
String line;
while ((line = br.readLine()) != null) {
String[] st = line.split(",");
if (st.length != 4) {
System.out.println("Error in given data");
continue;
}
long ts = Long.parseLong(st[0].trim());
String cId = st[1];
String path = st[2];
long latency = Long.parseLong(st[3].trim());
result.add(new VisitedRawData(ts, cId, path, latency));
}
}
return result;
}
public static List<VisitedLogData> parseVisitedLogData(
List<VisitedRawData> rawDataList) {
List<VisitedLogData> result = new ArrayList<>();
for (VisitedRawData vRData : rawDataList)
result.add(new VisitedLogData(vRData.customerId, vRData.path));
Collections.sort(result, VisitedLogData.getVisitedLogDataComparator());
return result;
}
public static Collection<List<String>> groupPathsByCustomers(
List<VisitedLogData> vDataList) {
if (vDataList.isEmpty())
return Collections.emptyList();
Map<String, List<String>> customerIdAndPathsList = new HashMap<String, List<String>>();
for (VisitedLogData vLogData : vDataList) {
if (!customerIdAndPathsList.containsKey(vLogData.customerId))
customerIdAndPathsList.put(vLogData.customerId,
new ArrayList<String>());
customerIdAndPathsList.get(vLogData.customerId).add(vLogData.path);
}
return customerIdAndPathsList.values();
}
public static Map<Path, AtomicInteger> groupPathByVisitedCount(
Collection<List<String>> pathsList) {
Map<Path, AtomicInteger> result = new HashMap<>();
for (List<String> component : pathsList) {
for (int i = 0; i < component.size() && (i + 2) < component.size(); i++) {
Path path = new Path(component.get(i), component.get(i + 1),
component.get(i + 2));
if (!result.containsKey(path))
result.put(path, new AtomicInteger(0));
result.get(path).incrementAndGet();
}
}
return result;
}
public static List<PathAndVisitedCount> findMaximumVisitedPath(
Map<Path, AtomicInteger> input) {
int maxVisited = -1;
List<PathAndVisitedCount> result = new ArrayList<>();
for (Map.Entry<Path, AtomicInteger> entry : input.entrySet()) {
if (maxVisited < entry.getValue().get()) {
result.clear();
maxVisited = entry.getValue().get();
result.add(new PathAndVisitedCount(entry.getKey(), maxVisited));
} else if (maxVisited == entry.getValue().get()) {
result.add(new PathAndVisitedCount(entry.getKey(), maxVisited));
}
}
return result;
}
}
class PathAndVisitedCount {
public final Path path;
public final int visitedCount;
public PathAndVisitedCount(Path path, int visitedCount) {
this.path = path;
this.visitedCount = visitedCount;
}
@Override
public String toString() {
return path.toString() + "," + "count : " + visitedCount;
}
}
class VisitedRawData {
public final long ts;
public final String customerId;
public final String path;
public final long latency;
public VisitedRawData(long ts, String customerId, String path, long latency) {
this.ts = ts;
this.customerId = customerId;
this.path = path;
this.latency = latency;
}
}
class VisitedLogData {
public final String customerId;
public final String path;
public VisitedLogData(String customerId, String path) {
this.customerId = customerId;
this.path = path;
}
@Override
public String toString() {
return customerId + "," + path;
}
public static Comparator<VisitedLogData> getVisitedLogDataComparator() {
return new Comparator<VisitedLogData>() {
@Override
public int compare(VisitedLogData vFirst, VisitedLogData vSecond) {
return vFirst.customerId.compareTo(vSecond.customerId);
}
};
}
}
class Path {
private final String p1, p2, p3;
public Path(String p1, String p2, String p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
@Override
public boolean equals(Object another) {
if (another == null || !(another instanceof Path))
return false;
Path other = (Path) another;
return p1.equals(other.p1) && p2.equals(other.p2) && p3.equals(p3);
}
@Override
public int hashCode() {
return Objects.hash(p1, p2, p3);
}
@Override
public String toString() {
return p1 + "," + p2 + "," + p3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment