Code · Interview problems · Problems
Task / Sub-task Hierarchy
Question
Topic: Hashing / Sorting · Difficulty: Medium
Read lines from stdin, each either a task or a sub-task:
date,task,taskId,name
date,subtask,subTaskId,parentTaskId,name
Print the tasks ordered by date (then id). Under each task, print its sub-tasks in the same order, using a tree prefix:
|- for every sub-task except the last, and \- for the last.
Approach: HashMap<parentId, List<subtask>> plus a list of tasks. Sort both by (date, id) and print.
BuildTaskAndSubTask.java 54 lines GitHub ↗
/**
* Problem: Task / Sub-task Hierarchy
* Difficulty: Medium
* Topic: Hashing / Sorting
* Question: Print tasks by date with their sub-tasks underneath, using |- and \- tree prefixes. Full statement in README.md.
*/
package dsa.question8;
import java.util.*;
public class BuildTaskAndSubTask {
public static class Item {
private String date, id, name;
public Item(String date, String id, String name) {
this.date = date; this.id = id ; this.name = name;
}
}
public static List<String> printTaskHierarchy(String[] lines) {
int n = lines.length , i=0;
HashMap<String, List<Item>> subTaskAndTaskMapping = new HashMap<>();
List<Item> tasks = new ArrayList<>();
for(i=0;i<n;i++) {
String[] line = lines[i].split(",");
String taskType = line[1];
if(taskType.equalsIgnoreCase("subtask")) { // meaning subtask
String parentTaskId = line[3] , subTaskId = line[2] , date = line[0] , name = line[4];
Item subTask = new Item(date, subTaskId , name);
subTaskAndTaskMapping.computeIfAbsent(parentTaskId , k -> new ArrayList<>()).add(subTask);
}else{
String taskId = line[2] , date = line[0] , name = line[3];
Item task = new Item(date, taskId , name);
tasks.add(task);
}
}
// sort on task
tasks.sort(Comparator.comparing( (Item a) -> a.date)
.thenComparing(a -> a.id));
List<String> ans = new ArrayList<>();
for(i=0;i<tasks.size();i++) {
ans.add(tasks.get(i).name);
if(subTaskAndTaskMapping.containsKey(tasks.get(i).id)) {
List<Item> subTaskList = subTaskAndTaskMapping.get(tasks.get(i).id);
subTaskList.sort(Comparator.comparing( (Item a) -> a.date)
.thenComparing(a -> a.id));
for(int j=0; j<subTaskList.size(); j++) {
if(j == subTaskList.size()-1) {
ans.add("\\- " + subTaskList.get(j).name);
}else{
ans.add("|- " + subTaskList.get(j).name);
}
}
}
}
return ans;
}
} Demo.java 26 lines GitHub ↗
package dsa.question8;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
List<String> input = new ArrayList<>();
String line;
while( (line = br.readLine()) != null) {
if(line.isEmpty()) {
continue;
}
input.add(line);
}
List<String> result = BuildTaskAndSubTask.printTaskHierarchy(input.toArray(new String[0]));
StringBuilder sb = new StringBuilder();
for(String s : result) {
sb.append(s);
sb.append('\n');
}
System.out.println(sb);
}
}