Suppose we want to monitor how locks are used in our system. As the first step, we log moments of acquire and release for each lock in the following format:
- ACQUIRE X
- RELEASE X
where X is some integer ID (1 <= X <= 1,000,000) of the lock.
All locks must be released in the reverse order of acquiring them; for example, this is a correct event sequence:
1. ACQUIRE 364
2. ACQUIRE 84
3. RELEASE 84
4. ACQUIRE 1337
5. RELEASE 1337
6. RELEASE 364
The following sequence violates this rule because lock 84 is still acquired while releasing lock 364:
1. ACQUIRE 364
2. ACQUIRE 84
3. RELEASE 364
4. RELEASE 84
It's also dangerous to leave locks acquired after application termination, as other processes in the system may be blocked while waiting on them, so such a sequence is incorrect, too:
1. ACQUIRE 364
2. ACQUIRE 84
3. RELEASE 84
since lock 364 is never released.
Another problem is lock misuse: releasing a lock that was never acquired:
1. ACQUIRE 364
2. RELEASE 84
3. RELEASE 364
And it is also invalid to acquire an already acquired lock (usually resulting in a deadlock):
1. ACQUIRE 364
2. ACQUIRE 84
3. ACQUIRE 364
4. RELEASE 364
Task
Write a program that, given a list of N (0 <= N <= 1,000,000) lock acquire and release events (counting from 1), checks if there were any problems (acquire-release order violation, dangling acquired lock, acquiring a lock twice or releasing a free lock), and if so, tells the earliest time that could be detected.
You are given an array of strings where each string is either "ACQUIRE X"
or "RELEASE X"
, where all Xs are integers in the range [1..1000000].
Return
- 0, if there were no lock-related problems even after program termination
- N + 1, if the only issue after program termination was dangling acquired locks
- K, in case event number K violated any of the principles (release a lock not acquired previously, acquire an already held lock OR violate lock acquire-release order)
Count Palindromic Substrings
Problem Description
A string S
is called a palindrome if it reads the same backward as forward. Any non-empty string has substrings that are palindromes. For example, for S = "hellolle"
, valid palindromic substrings include:
"ellolle"
"ll"
— appears more than once at different positions"lol"
and"lloll"
- and all single characters (8 of them in this example)
Write a function that, given a string S
consisting of only lowercase English letters, counts how many different ways there are to pick a palindrome substring from S
.
Input
- A string
S
(1 ≤ |S| ≤ 1000), consisting of lowercase English letters only.
Output
- An integer representing the total number of palindromic substrings (counting each distinct position, not just value).
Example
def count_palindromic_substrings(S):
n = len(S)
count = 0
for center in range(2 * n - 1):
l = center // 2
r = l + (center % 2)
while l >= 0 and r < n and S[l] == S[r]:
count += 1
l -= 1
r += 1
return count
# Example
print(count_palindromic_substrings("hellolle")) # Output: total count of palindromic substrings
Given a binary search implementation, write a verification program that:
- prints
"CORRECT"
if the binary search is implemented properly - or prints a counter-example:
- first line: number of elements in the array
- second line: space-separated sorted array
- third line: a target value for which the implementation returns an incorrect result
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.
