Java Interview Practice Problem (Beginner): Partition Logs

  • Post last modified:September 21, 2023
  • Reading time:5 mins read

Problem

  • We have been given a List of Application Logs which has properties such as id, message, tags, and datetime.
  • We have been given the task of partitioning these logs by created datetime. 

Example,

[ApplicationLog[id=c1e1838c-1c17-4e22-a476-c604dc3dadcc, message=Logs 1, tags=[tag-1, server-4, tag-1], date=Sat Sep 16 21:48:36 EDT 2023], ApplicationLog[id=ca1f45ec-8444-4cb5-8695-08147519080f, message=Logs 2, tags=[data-1, server-1, data-2], date=Sun Sep 10 21:48:36 EDT 2023], ApplicationLog[id=d93a32c1-1e11-4155-992c-1ea70052f3c0, message=Logs 3, tags=[tag-1, tag-4, data-1], date=Mon Sep 18 21:48:36 EDT 2023], 
.....

Output

2023-09-19:[ApplicationLog[id=794fe57d-5d92-4bc3-ab62-11e9aa2d7640, message=Logs 6, tags=[server-2, server-3, data-4], date=Tue Sep 19 21:48:36 EDT 2023], ApplicationLog[id=f58b6c6f-bcd9-4cbd-9f56-e73ac951a1ac, message=Logs 21, tags=[tag-4, tag-4, tag-2], date=Tue Sep 19 21:48:36 EDT 2023], ....]
2023-09-17:[ApplicationLog[id=e531feff-8da8-4155-8b3d-87fea2f1fd33, message=Logs 37, tags=[tag-1, tag-1, server-1], date=Sun Sep 17 21:48:36 EDT 2023],...] 
....

Before jumping to the solution consider giving an attempt

Solution

Input Data

  • For input data, we will create a Java record class as dto.
  • For record dto, we are constructing each property using the Random method.
  • Our solution streams over input data and groups records using the date field.
  • Since our date is in datetime format, we need to convert it yyyy-MM-dd.
  • For grouping records we can use groupingBy method provided by Collectors API.
  • Now we can print date and grouped logs as output.
2023-09-19:[ApplicationLog[id=794fe57d-5d92-4bc3-ab62-11e9aa2d7640, message=Logs 6, tags=[server-2, server-3, data-4], date=Tue Sep 19 21:48:36 EDT 2023], ApplicationLog[id=f58b6c6f-bcd9-4cbd-9f56-e73ac951a1ac, message=Logs 21, tags=[tag-4, tag-4, tag-2], date=Tue Sep 19 21:48:36 EDT 2023],...]]
2023-09-17:[ApplicationLog[id=e531feff-8da8-4155-8b3d-87fea2f1fd33, message=Logs 37, tags=[tag-1, tag-1, server-1], date=Sun Sep 17 21:48:36 EDT 2023], ApplicationLog[id=28ba0809-2582-4b1a-85d8-6072d2b78afa, message=Logs 41, tags=[tag-1, server-1, data-4], date=Sun Sep 17 21:48:36 EDT 2023], ....]]
2023-09-18:[ApplicationLog[id=d93a32c1-1e11-4155-992c-1ea70052f3c0, message=Logs 3, tags=[tag-1, tag-4, data-1], date=Mon Sep 18 21:48:36 EDT 2023], ApplicationLog[id=f8ed7ca5-ef60-4bdb-a96b-81707e457325, message=Logs 14, tags=[data-1, server-4, server-3], date=Mon Sep 18 21:48:36 EDT 2023], ApplicationLog[id=e79c5028-cf92-467c-b3d8-a9aad4a58b68, message=Logs 22, tags=[tag-1, server-3, server-3], date=Mon Sep 18 21:48:36 EDT 2023], ...]]
2023-09-15:[ApplicationLog[id=f9938c22-049b-4e65-9603-443370855cf2, message=Logs 4, tags=[server-4, tag-4, tag-4], date=Fri Sep 15 21:48:36 EDT 2023], ApplicationLog[id=9ce27be9-03ae-4e6b-9768-900eddd4986f, message=Logs 25, tags=[server-1, tag-4, server-3], date=Fri Sep 15 21:48:36 EDT 2023], ...]]
2023-09-16:[ApplicationLog[id=c1e1838c-1c17-4e22-a476-c604dc3dadcc, message=Logs 1, tags=[tag-1, server-4, tag-1], date=Sat Sep 16 21:48:36 EDT 2023], ApplicationLog[id=1ca79892-5442-46d1-98c2-e081689000cc, message=Logs 11, tags=[server-4, server-1, server-3], date=Sat Sep 16 21:48:36 EDT 2023], ApplicationLog[id=7d3263fe-7955-4011-9661-791532a4a312, message=Logs 12, tags=[server-2, server-2, server-4], date=Sat Sep 16 21:48:36 EDT 2023], ApplicationLog[id=5e7d4248-e2d7-45eb-9a40-957569d99840, message=Logs 17, tags=[server-1, tag-4, data-1], date=Sat Sep 16 21:48:36 EDT 2023], ApplicationLog[id=c7ffd117-0c25-4f6c-b8c4-5d802ab41605, message=Logs 18, tags=[server-3, server-2, tag-4], date=Sat Sep 16 21:48:36 EDT 2023], ...]]
2023-09-13:[ApplicationLog[id=000d7761-cbc5-407e-9290-38ce7f667d45, message=Logs 24, tags=[tag-1, data-2, data-4], date=Wed Sep 13 21:48:36 EDT 2023], ApplicationLog[id=979aef0d-a83b-437d-abac-10810837e61a, message=Logs 28, tags=[server-4, server-4, server-2], date=Wed Sep 13 21:48:36 EDT 2023], ...]]
2023-09-14:[ApplicationLog[id=82b129e2-e349-44f7-aaea-4ce026c2ed5f, message=Logs 5, tags=[tag-4, data-4, server-2], date=Thu Sep 14 21:48:36 EDT 2023], ApplicationLog[id=90042b2a-0e4b-4acb-a8e2-31ba1564fc65, message=Logs 8, tags=[data-4, server-1, tag-1], date=Thu Sep 14 21:48:36 EDT 2023], ApplicationLog[id=46c32ee4-cb30-456d-8dfc-376973d9fa91, message=Logs 19, tags=[tag-2, server-1, server-3], date=Thu Sep 14 21:48:36 EDT 2023], ....]]
2023-09-11:[ApplicationLog[id=76356929-ee3a-4c9d-9574-d11067af2238, message=Logs 7, tags=[data-2, tag-1, data-4], date=Mon Sep 11 21:48:36 EDT 2023], ApplicationLog[id=33a56e21-4cbc-4ab8-b592-ff12b18bc4a8, message=Logs 13, tags=[tag-4, tag-4, server-4], date=Mon Sep 11 21:48:36 EDT 2023], ApplicationLog[id=a55eac9a-2c58-4887-81bd-2538307daa86, message=Logs 15, tags=[tag-1, tag-2, server-2], date=Mon Sep 11 21:48:36 EDT 2023], ApplicationLog[id=2e1d58d0-145a-48db-939b-a4e1486829ab, message=Logs 38, tags=[tag-2, tag-4, server-1], date=Mon Sep 11 21:48:36 EDT 2023], ApplicationLog[id=76f24fdc-227c-4951-9dd6-62ad2d3309ac, message=Logs 43, tags=[data-2, server-1, server-3], date=Mon Sep 11 21:48:36 EDT 2023],.....]
2023-09-12:[ApplicationLog[id=224c50ec-bb97-4a96-a68b-74b5f6b0a4f8, message=Logs 9, tags=[data-2, tag-4, server-1], date=Tue Sep 12 21:48:36 EDT 2023], ApplicationLog[id=569b0a95-7571-4ae9-9632-0693985429ad, message=Logs 47, tags=[data-4, data-2, server-4], date=Tue Sep 12 21:48:36 EDT 2023], ....]]
2023-09-10:[ApplicationLog[id=ca1f45ec-8444-4cb5-8695-08147519080f, message=Logs 2, tags=[data-1, server-1, data-2], date=Sun Sep 10 21:48:36 EDT 2023], ApplicationLog[id=c41a20fe-3324-44f0-9c90-248faf127a46, message=Logs 10, tags=[server-4, server-2, server-2], date=Sun Sep 10 21:48:36 EDT 2023], ApplicationLog[id=4743e971-e32c-4957-9469-42aa0ecd5fc4, message=Logs 16, tags=[server-1, data-2, data-2], date=Sun Sep 10 21:48:36 EDT 2023] ...]]

Code

Find all the code for this & other exercises on GitHub

Over to you! Write down your solution in the comment section for others to get back to it

Before You Leave

Other Problems List

Leave a Reply