Log Entry Locator

🎯 Level: Easy Score: 30 ⏱️ Estimated Time: 30 mins
📜 Problem Description

In a large-scale server environment, monitoring system logs is crucial for identifying anomalies, debugging issues, and ensuring smooth operations. These logs can be massive, containing millions of entries. Your task is to develop a utility that helps system administrators quickly locate specific event patterns or error signatures within a much larger master log.

Given two strings, a master_log representing the full log content and a query_pattern representing the specific event or error signature you are looking for, your goal is to find the starting index of the first occurrence of the query_pattern as a continuous substring within the master_log.

If the query_pattern does not exist anywhere in the master_log, you should return -1.

Consider the master_log and query_pattern to consist only of lowercase English alphabets.

Input Format

The first line contains an integer T, indicating the number of test cases.

For each test case:

  • The first line contains the master_log string.
  • The second line contains the query_pattern string.

Output Format

For each test case, output a single integer:

  • The 0-based index of the first character of the query_pattern in the master_log if found.
  • -1 if the query_pattern is not found.

Constraints

  • 1 <= T <= 50
  • 1 <= master_log.length, query_pattern.length <= 1000
  • Both master_log and query_pattern consist only of lowercase English alphabets ('a'-'z').
📌 Constraints

                    
📝 Sample Input/Output
Example 1: Successful Log Pattern Match

An administrator is scanning for a "connection reset" error.

master_log: "server_boot_successful_connection_reset_error_code_500"
query_pattern: "connection_reset"
Output: 22

Explanation: The string "connection_reset" starts at index 22 of the master log "server_boot_successful_connection_reset_error_code_500".

Example 2: Pattern Not Found

Searching for a specific database error that didn't occur.

master_log: "webserver_activity_log_session_id_12345"
query_pattern: "database_error"
Output: -1

Explanation: The string "database_error" is not present in "webserver_activity_log_session_id_12345".

Example 3: Pattern at the Beginning

The log starts with the expected server initialization message.

master_log: "system_init_complete_process_start"
query_pattern: "system_init"
Output: 0

Explanation: The string "system_init" starts at index 0 of the master log.

💡 Hints
  • 💡 Consider iterating through the `master_log` and, at each position, checking if the `query_pattern` matches the substring starting from that position.
  • 💡 Pay attention to the lengths of both strings to avoid out-of-bounds errors. If the `query_pattern` is longer than the remaining part of the `master_log`, it cannot be found.
  • 💡 A nested loop structure can be used: an outer loop for potential starting positions in the `master_log` and an inner loop for comparing characters of the `query_pattern`.
📚 Topics

String & Tries

🏢 Asked By Companies

Adobe

😕 No submissions found

Start coding and your submissions will appear here.