Full Question — IP Request Validation System You ...

Créé le : 7 octobre 2026

Répondu en utilisant GPT-5.6 Thinking par Chat01

Question

Full Question — IP Request Validation System

You are given an IP request validation system. The system has two rules for validating requests:

  1. Block the request if the IP matches any blacklisted regex pattern.
  2. Block the request if the IP has sent at least 2 unblocked requests in the last 5 seconds.

Blacklisted IP patterns

Blacklisted regex patterns can contain:

  • Numbers
  • Dots .
  • Wildcards *

The wildcard * represents one or more characters.

IPs are formatted as strings of four integers separated by three dots:

<number>.<number>.<number>.<number>


Example

n = 3

blacklisted_ips = ["m*.", "123.", "34.*"]

q = 7

requests = [
"123.1.23.34",
"121.1.23.34",
"121.1.23.34",
"34.1.23.34",
"121.1.23.34",
"12.1.23.34",
"121.1.23.34"
]

The requests arrive at times 1 through 7 respectively.

Request processing

Time Request IP Matching Blacklisted IP Number of unblocked requests from this IP in last 5 sec Blocked

1 123.1.23.34 123.* 0 1
2 121.1.23.34 - 0 0
3 121.1.23.34 - 1 0
4 34.1.23.34 34.* 0 1
5 121.1.23.34 - 2 1
6 12.1.23.34 - 0 0
7 121.1.23.34 - 1 0

Therefore:

answer = [1, 0, 0, 1, 1, 0, 0]

Important detail

At time 7, the request

121.1.23.34

is not blocked because in the last 5 seconds only the request that came at time 3 is counted.

The request that came at time 5 was blocked, so it is not included in the count of unblocked requests.


Function Description

Complete the function:

vector<int> validateRequests(
vector<string> blacklisted_ips,
vector<string> requests
)

Parameters

  • blacklisted_ips: a string array containing the blacklisted IP regexes.
  • requests: a string array containing the IPs of incoming requests.

Returns

int[]

For each request:

  • Return 1 if the request is blocked.
  • Return 0 otherwise.

Constraints

1 <= n <= 10
1 <= length of blacklisted_ips[i] <= 15
1 <= q <= 1000
1 <= length of requests[i] <= 15

Sample Input

2
111..255
12.

4
121.3.5.255
12.13.5.255
111.3.5.255
121.3.5.255

Sample Output

0
0
1
0

The third request is blocked because it matches:

111.*.255

All other requests can be processed.

Solved this but passed 13/15
#include <bits/stdc++.h>
using namespace std;

bool match(string s, string p) {
int n = s.size(), m = p.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1));

text
dp[0][0] = 1; for (int i = 0; i <= n; i++) { for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') { if (i > 0) dp[i][j] = dp[i - 1][j] || dp[i - 1][j - 1]; } else if (i > 0 && s[i - 1] == p[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } } } return dp[n][m];

}

vector<int> validateRequests(vector<string> blacklisted_ips, vector<string> requests) {
unordered_map<string, deque<int>> mp;
vector<int> ans;

text
for (int i = 0; i < (int)requests.size(); i++) { string ip = requests[i]; bool blocked = false; for (string p : blacklisted_ips) { if (match(ip, p)) { blocked = true; break; } } if (!blocked) { auto &q = mp[ip]; while (!q.empty() && q.front() <= i - 5) q.pop_front(); if (q.size() >= 2) blocked = true; else q.push_back(i); } ans.push_back(blocked ? 1 : 0); } return ans;

}

int main() {
vector<string> b = {"m*.", "123.", "34.*"};
vector<string> r = {
"123.1.23.34",
"121.1.23.34",
"121.1.23.34",
"34.1.23.34",
"121.1.23.34",
"12.1.23.34",
"121.1.23.34"
};

text
vector<int> ans = validateRequests(b, r); for (int x : ans) cout << x << '\n'; return 0;

}

Réponse

Your rate-limit logic is basically right. The likely hidden-test failure is in the wildcard matcher, so I’m checking its exact * = one or more characters behavior and edge cases rather than changing the 5-second window blindly.

Réflexion

Resolving wildcard semantics

I’m spotting a likely inconsistency: under ordinary regex rules, 12.* should match 12.13.5.255, so the reported zero seems unexpected. I’m checking whether the problem defines * differently or whether the sample contains a typo.

Resolved wildcard semantics

Réflexion

Searched 7 websites

Python

python
def m1(s,p): n,m=len(s),len(p) dp=[[0]*(m+1) for _ in range(n+1)] dp[0][0]=1 for i in range(n+1): for j in range(1,m+1): if p[j-1]=='*': if i>0: dp[i][j]=dp[i-1][j] or dp[i-1][j-1] elif i>0 and s[i-1]==p[j-1]: dp[i][j]=dp[i-1][j-1] return bool(dp[n][m]) from functools import lru_cache def m2(s,p): @lru_cache(None) def f(i,j): if j==len(p): return i==len(s) if p[j]=='*': # consume one or more return i < len(s) and (f(i+1,j) or f(i+1,j+1)) return i<len(s) and s[i]==p[j] and f(i+1,j+1) return f(0,0) import itertools, random chars='12.' for ls in range(5): for lp in range(5): for s in map(''.join,itertools.product(chars, repeat=ls)): for p in map(''.join,itertools.product(chars+'*', repeat=lp)): if m1(s,p)!=m2(s,p): print("diff",s,p,m1(s,p),m2(s,p)); raise SystemExit print("all same")

Réflexion

Verified wildcard matching against recursion

Réflexion

Comparing wildcard rules

I’m narrowing the discrepancy to wildcard semantics: if * means zero or more characters, the implementation requiring at least one character can fail hidden cases. However, 12.* should still match the stated address, suggesting the sample may also contain an error.

Compared wildcard rules

Réflexion

Clarifying the mismatch

I’m concluding that the pasted specification and sample conflict: 12.* should match that address. The likely hidden-test issue is that * allows zero characters, while the implementation may require at least one.

Pinpointing the fix

I’m identifying two issues: * likely needs zero-or-more matching, using dp[i][j-1] or dp[i-1][j]; separately, the pasted sample is inconsistent because 12.* should match 12.13.5.255 under either wildcard interpretation.

Clarified the mismatch and pinpointed the fix

Searched 23 websites

Worked for 1m 18s

Réponse

I found the likely reason you’re getting 13/15. Your rate-limiting logic is correct; the issue is almost certainly the semantics of *.

The publicly posted versions of this UBS OA problem specify that * matches zero or more characters, not one or more. (LeetCode)

Bug in your matcher

Your current transition:

cpp
if (p[j - 1] == '*') { if (i > 0) dp[i][j] = dp[i - 1][j] || dp[i - 1][j - 1]; }

forces * to consume at least one character.

For * = zero or more characters, you need:

cpp
dp[i][j] = dp[i][j - 1] || (i > 0 && dp[i - 1][j]);

The two cases are:

text
dp[i][j - 1] -> '*' matches zero characters dp[i - 1][j] -> '*' consumes one more character

So use this:

cpp
bool match(const string& s, const string& p) { int n = s.size(), m = p.size(); vector<vector<bool>> dp(n + 1, vector<bool>(m + 1, false)); dp[0][0] = true; for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') dp[0][j] = dp[0][j - 1]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') { // zero chars OR one/more chars dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } else if (s[i - 1] == p[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } } } return dp[n][m]; }

Your validateRequests() can otherwise stay essentially unchanged:

cpp
vector<int> validateRequests( vector<string> blacklisted_ips, vector<string> requests ) { unordered_map<string, deque<int>> mp; vector<int> ans; for (int i = 0; i < requests.size(); i++) { const string& ip = requests[i]; bool blocked = false; // Rule 1: blacklist for (const string& p : blacklisted_ips) { if (match(ip, p)) { blocked = true; break; } } // Rule 2: rate limiting if (!blocked) { auto& q = mp[ip]; // Requests exactly 5 seconds old are NOT counted. while (!q.empty() && q.front() <= i - 5) q.pop_front(); if (q.size() >= 2) { blocked = true; } else { // Only store UNBLOCKED requests. q.push_back(i); } } ans.push_back(blocked ? 1 : 0); } return ans; }

Your 5-second logic is correct

This part:

cpp
while (!q.empty() && q.front() <= i - 5) q.pop_front();

is correct.

At time 7 (i = 6 zero-based):

text
i - 5 = 1

so the request at zero-based index 1 (time 2) gets removed, while the request at index 2 (time 3) remains.

And you're correctly not inserting blocked requests:

cpp
if (q.size() >= 2) blocked = true; else q.push_back(i);

That's exactly why the blocked request at time 5 doesn't affect time 7.

One more thing: your pasted sample is inconsistent

You have:

text
blacklist: 12.* request: 12.13.5.255

Under either “one or more” or “zero or more” wildcard semantics, 12.* matches 12.13.5.255, so that request should be blocked. Thus the sample output you pasted cannot simultaneously be correct with the stated wildcard rules; there's likely a transcription error in that sample.

For the 13/15 issue, however, I'd submit the zero-or-more * version above. That's the semantics found in the available versions of this exact UBS validation problem. (LeetCode)

Partager cette Q&R