Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions src/main/java/datastructures/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package datastructures;

public class Event {

int timestamp;
String category;
String displayTest;

public Event(int timestamp, String category, String displayTest) {
this.timestamp = timestamp;
this.category = category;
this.displayTest = displayTest;
}

public int getTimestamp() {
return timestamp;
}

public void setTimestamp(int timestamp) {
this.timestamp = timestamp;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getDisplayTest() {
return displayTest;
}

public void setDisplayTest(String displayTest) {
this.displayTest = displayTest;
}
}
42 changes: 42 additions & 0 deletions src/main/java/googleOA/socialMedia/SocialMedia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package googleOA.socialMedia;

import datastructures.Event;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SocialMedia {

Map<String, Integer> categoryLimit =
Map.of("USER_FOLLOW", 1, "TRENDING_TOPIC", 2, "DIRECT_MESSAGE", 5);
public List<String> collapseEvents(List<Event> events) {
List<String> res = new ArrayList<>();
String previousCategory = null;
int previousCategoryCount = 0;

for (Event event : events) {
if (event.getCategory().equals(previousCategory))
previousCategoryCount++;
else {
if (previousCategory != null) {
if (categoryLimit.containsKey(previousCategory)) {
if (previousCategoryCount > categoryLimit.get(previousCategory)) {
String val = previousCategory + "(+" + String.valueOf(previousCategoryCount - 1) + " more)";
res.add(val);
} else {
for (int i = 0; i < previousCategoryCount; i++) {
res.add(previousCategory);
}
}
}
}
previousCategoryCount = 1;
}
previousCategory = event.getCategory();
if (previousCategoryCount == 0) previousCategoryCount++;
}

return res;
}
}
57 changes: 57 additions & 0 deletions src/test/java/googleOA/socialMedia/SocialMediaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package googleOA.socialMedia;

import datastructures.Event;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class SocialMediaTest {

private SocialMedia socialMedia;

private List<Event> testcase = new ArrayList<>();

@BeforeEach
void setUp() {
socialMedia = new SocialMedia();
Event first = new Event(1, "USER_FOLLOW", "");
Event sec = new Event(1, "USER_FOLLOW", "");
Event third = new Event(1, "TRENDING_TOPIC", "");
Event fourth = new Event(1, "TRENDING_TOPIC", "");
Event fifth = new Event(1, "TRENDING_TOPIC", "");
Event sixth = new Event(1, "TRENDING_TOPIC", "");
Event seventh = new Event(1, "TRENDING_TOPIC", "");
Event eighth = new Event(1, "TRENDING_TOPIC", "");
Event nineth = new Event(1, "TRENDING_TOPIC", "");
Event tenth = new Event(1, "DIRECT_MESSAGE", "");
Event eleventh = new Event(1, "DIRECT_MESSAGE", "");
Event twelveth = new Event(1, "DIRECT_MESSAGE", "");
Event thirteenth = new Event(1, "DIRECT_MESSAGE", "");
Event fourteenth = new Event(1, "DIRECT_MESSAGE", "");
Event fifteenth = new Event(1, "DIRECT_MESSAGE", "");
Event sixteenth = new Event(1, "TRENDING_TOPIC", "");

testcase = List.of(first, sec, third, fourth, fifth, sixth, seventh, eighth,
nineth, tenth, eleventh, twelveth, thirteenth, fourteenth, fifteenth, sixteenth);
}

@Test
void collapseEvents() {
List<String> expectedResult = List.of(
"USER_FOLLOW",
"USER_FOLLOW",
"TRENDING_TOPIC (+6 more)",
"DIRECT_MESSAGE (+5 more)",
"TRENDING_TOPIC"
);

List<String> actualResult = socialMedia.collapseEvents(testcase);
for (int i = 0; i < actualResult.size(); i++) {
assertEquals(expectedResult.get(i), actualResult.get(i));
}
}
}