Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

635. Design Log Storage System

Explanation:

To design a log storage system, we can use a data structure that allows us to store logs and query logs based on a given range of timestamps. One approach is to use a TreeMap where we store the logs with their timestamps as keys. This will allow us to efficiently query logs within a given time range.

Algorithmic Idea:

  1. Use a TreeMap to store logs with timestamps as keys.
  2. For each log operation, insert the log with its timestamp into the TreeMap.
  3. When querying logs within a time range, iterate through the TreeMap and filter logs within the given range.

Time Complexity:

  • Inserting a log: O(log n) where n is the number of logs.
  • Querying logs within a time range: O(n) where n is the number of logs within the given range.

Space Complexity:

  • O(n) where n is the number of logs stored in the TreeMap.

: :

import java.util.TreeMap;

class LogSystem {
    TreeMap<Long, String> logs;

    public LogSystem() {
        logs = new TreeMap<>();
    }

    public void put(int id, String timestamp) {
        logs.put(Long.parseLong(timestamp.replace(":", "")), String.valueOf(id));
    }

    public List<Integer> retrieve(String s, String e, String gra) {
        long start = getStartTime(s, gra);
        long end = getEndTime(e, gra);
        List<Integer> result = new ArrayList<>();

        for (Map.Entry<Long, String> entry : logs.subMap(start, true, end, true).entrySet()) {
            result.add(Integer.parseInt(entry.getValue()));
        }

        return result;
    }

    private long getStartTime(String timestamp, String gra) {
        timestamp = timestamp.replace(":", "");
        if (gra.equals("Year")) {
            return Long.parseLong(timestamp.substring(0, 4) + "0000000000");
        } else if (gra.equals("Month")) {
            return Long.parseLong(timestamp.substring(0, 6) + "00000000");
        } else if (gra.equals("Day")) {
            return Long.parseLong(timestamp.substring(0, 8) + "000000");
        } else if (gra.equals("Hour")) {
            return Long.parseLong(timestamp.substring(0, 10) + "0000");
        } else if (gra.equals("Minute")) {
            return Long.parseLong(timestamp.substring(0, 12) + "00");
        } else {
            return Long.parseLong(timestamp);
        }
    }

    private long getEndTime(String timestamp, String gra) {
        timestamp = timestamp.replace(":", "");
        if (gra.equals("Year")) {
            return Long.parseLong(timestamp.substring(0, 4) + "1231235959");
        } else if (gra.equals("Month")) {
            return Long.parseLong(timestamp.substring(0, 6) + "31235959");
        } else if (gra.equals("Day")) {
            return Long.parseLong(timestamp.substring(0, 8) + "235959");
        } else if (gra.equals("Hour")) {
            return Long.parseLong(timestamp.substring(0, 10) + "5959");
        } else if (gra.equals("Minute")) {
            return Long.parseLong(timestamp.substring(0, 12) + "59");
        } else {
            return Long.parseLong(timestamp);
        }
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.