Created
August 28, 2025 19:48
-
-
Save youssef3wi/c3d56aac8c725204dd14044acaceb1b0 to your computer and use it in GitHub Desktop.
You collected data about persons from multiple sources. You would like to consolidate them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Set; | |
| import java.util.TreeMap; | |
| import java.util.TreeSet; | |
| /** | |
| * You collected data about persons from multiple sources. You would like to consolidate them. | |
| * <p> | |
| * Your data is organized in a list of strings. | |
| * Each one corresponds to a single person, and is organized in key/value pairs. | |
| * Here is an example showing the format: | |
| * <ul> | |
| * <li>{@code Name=John;Age=15;Likes=Apples}</li> | |
| * <li>{@code Name=Mary;Age=16;Likes=Baked potatoes;Team=Basketball}</li> | |
| * </ul> | |
| * You have some guarantees on the input data: | |
| * <ul> | |
| * <li>Each string will always contain the "{@code Name}" field.</li> | |
| * <li>Each person can be uniquely identified by their name. | |
| * If there are multiple lines with the same value of Name, they are all about the same person.</li> | |
| * <li>There may be redundant information between strings, | |
| * or even inside a single string, but they will never contradict each other.</li> | |
| * <li>Keys and values can contain any printable ASCII characters, | |
| * except {@code =} (equal sign) and {@code ;} (semicolon), which are used as separators.</li> | |
| * <li>Each string does not contain any line break ({@code \r} or {@code \n})</li> | |
| * </ul> | |
| * You have to merge the data and to produce a list gathering everything about each person. | |
| * It should be formatted in the following way: | |
| * <ul> | |
| * <li>All persons are sorted lexicographically by name.</li> | |
| * <li>The first field for each person is "{@code Name}", then all other fields are sorted by key.</li> | |
| * </ul> | |
| * Note that these two constraints are not necessarily respected by the list you get in input. | |
| * | |
| * <h5>Constraints</h5> | |
| * <ul> | |
| * <li>There will be at most 100 strings in the list.</li> | |
| * <li>Each string has a maximum length of 1000 characters.</li> | |
| * <li>Each string to output will also have a maximum length of 1000 characters.</li> | |
| * <li>There are no empty names, so empty keys, no empty values.</li> | |
| * </ul> | |
| * <h5>Detailed example</h5> | |
| * Let's assume you receive the following strings. | |
| * <ul> | |
| * <li>{@code Name=John;Age=15;Likes=Apples}</li> | |
| * <li>{@code Name=Mary;Age=16;Likes=Baked potatoes;Team=Basketball}</li> | |
| * <li>{@code Name=Adam;Age=17;Score=133;Likes=Jellied eels}</li> | |
| * <li>{@code Name=John;Score=283;City=NYC}</li> | |
| * </ul> | |
| */ | |
| public class PersonCollector { | |
| /** | |
| * @param dataStrings [[anArray]] of strings, where each string represents a person. | |
| * @return [[anArray]] of strings, the merged input data. | |
| */ | |
| public static List<String> mergeData(List<String> dataStrings) { | |
| Map<String, String> entries = new TreeMap<>(); | |
| for (String current : dataStrings) { | |
| String[] parts = current.split(";"); | |
| Set<String> keys = new TreeSet<>(); | |
| for (int idx = 1; idx < parts.length; idx++) { | |
| keys.add(parts[idx]); | |
| } | |
| String name = parts[0].replace("Name=", ""); | |
| String existing = entries.get(name); | |
| if (existing != null) { | |
| parts = existing.split(";"); | |
| for (int idx = 1; idx < parts.length; idx++) { | |
| keys.add(parts[idx]); | |
| } | |
| } | |
| if (keys.isEmpty()) { | |
| current = String.format("Name=%s", name); | |
| } else { | |
| current = "Name=" + name + ";" + String.join(";", keys); | |
| } | |
| entries.put(name, current); | |
| } | |
| return new ArrayList<>(entries.values()); | |
| } | |
| public static void main(String[] args) { | |
| String[] values = new String[]{ | |
| "Name=John;Age=15;Likes=Apples", | |
| "Name=Mary;Age=16;Likes=Baked potatoes;Team=Basketball", | |
| "Name=Adam;Age=17;Score=133;Likes=Jellied eels", | |
| "Name=John;Score=283;City=NYC"}; | |
| System.out.println("Using example: " + mergeData(Arrays.stream(values).collect(ArrayList::new, ArrayList::add, ArrayList::addAll))); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment