Skip to content

Commit 77dc0ee

Browse files
authored
Added tasks 3673-3677
1 parent 8cdc57b commit 77dc0ee

File tree

15 files changed

+605
-0
lines changed

15 files changed

+605
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
3673\. Find Zombie Sessions
2+
3+
Hard
4+
5+
Table: `app_events`
6+
7+
+------------------+----------+
8+
| Column Name | Type |
9+
+------------------+----------+
10+
| event_id | int |
11+
| user_id | int |
12+
| event_timestamp | datetime |
13+
| event_type | varchar |
14+
| session_id | varchar |
15+
| event_value | int |
16+
+------------------+----------+
17+
event_id is the unique identifier for this table.
18+
event_type can be app_open, click, scroll, purchase, or app_close.
19+
session_id groups events within the same user session.
20+
event_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL.
21+
22+
Write a solution to identify **zombie sessions, **sessions where users appear active but show abnormal behavior patterns. A session is considered a **zombie session** if it meets ALL the following criteria:
23+
24+
* The session duration is **more than** `30` minutes.
25+
* Has **at least** `5` scroll events.
26+
* The **click-to-scroll ratio** is less than `0.20` .
27+
* **No purchases** were made during the session.
28+
29+
Return _the result table ordered by_ `scroll_count` _in **descending** order, then by_ `session_id` _in **ascending** order_.
30+
31+
The result format is in the following example.
32+
33+
**Example:**
34+
35+
**Input:**
36+
37+
app\_events table:
38+
39+
+----------+---------+---------------------+------------+------------+-------------+
40+
| event_id | user_id | event_timestamp | event_type | session_id | event_value |
41+
+----------+---------+---------------------+------------+------------+-------------+
42+
| 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL |
43+
| 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 |
44+
| 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 |
45+
| 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 |
46+
| 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 |
47+
| 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 |
48+
| 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 |
49+
| 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL |
50+
| 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL |
51+
| 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL |
52+
| 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 |
53+
| 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL |
54+
| 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 |
55+
| 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 |
56+
| 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL |
57+
| 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL |
58+
| 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 |
59+
| 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 |
60+
| 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL |
61+
| 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 |
62+
| 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 |
63+
| 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 |
64+
| 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL |
65+
| 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL |
66+
| 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 |
67+
| 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 |
68+
| 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL |
69+
| 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL |
70+
+----------+---------+---------------------+------------+------------+-------------+
71+
72+
**Output:**
73+
74+
+------------+---------+--------------------------+--------------+
75+
| session_id | user_id | session_duration_minutes | scroll_count |
76+
+------------+---------+--------------------------+--------------+
77+
| S001 | 201 | 35 | 6 |
78+
+------------+---------+--------------------------+--------------+
79+
80+
**Explanation:**
81+
82+
* **Session S001 (User 201)**:
83+
* Duration: 10:00:00 to 10:35:00 = 35 minutes (more than 30)
84+
* Scroll events: 6 (at least 5)
85+
* Click events: 0
86+
* Click-to-scroll ratio: 0/6 = 0.00 (less than 0.20)
87+
* Purchases: 0 (no purchases)
88+
* S001 is a zombie session (meets all criteria)
89+
* **Session S002 (User 202)**:
90+
* Duration: 11:00:00 to 11:20:00 = 20 minutes (less than 30)
91+
* Has a purchase event
92+
* S002 is not a zombie session
93+
* **Session S003 (User 203)**:
94+
* Duration: 12:00:00 to 13:00:00 = 60 minutes (more than 30)
95+
* Scroll events: 5 (at least 5)
96+
* Click events: 1
97+
* Click-to-scroll ratio: 1/5 = 0.20 (not less than 0.20)
98+
* Purchases: 0 (no purchases)
99+
* S003 is not a zombie session (click-to-scroll ratio equals 0.20, needs to be less)
100+
* **Session S004 (User 204)**:
101+
* Duration: 14:00:00 to 14:12:00 = 12 minutes (less than 30)
102+
* Scroll events: 2 (less than 5)
103+
* S004 is not a zombie session
104+
105+
The result table is ordered by scroll\_count in descending order, then by session\_id in ascending order.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Write your MySQL query statement below
2+
# #Hard #Database #2025_09_07_Time_278_ms_(100.00%)_Space_0.0_MB_(100.00%)
3+
SELECT
4+
session_id,
5+
user_id,
6+
TIMESTAMPDIFF(MINUTE, MIN(event_timestamp), MAX(event_timestamp)) AS session_duration_minutes,
7+
SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END) AS scroll_count -- NOSONAR
8+
FROM
9+
app_events
10+
GROUP BY
11+
session_id,
12+
user_id
13+
HAVING
14+
TIMESTAMPDIFF(MINUTE, MIN(event_timestamp), MAX(event_timestamp)) > 30
15+
AND SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END) > 4 -- NOSONAR
16+
AND (
17+
CAST(SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END) AS DOUBLE) /
18+
NULLIF(SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END), 0) -- NOSONAR
19+
) < 0.2
20+
AND SUM(CASE WHEN event_type = 'purchase' THEN 1 ELSE 0 END) = 0
21+
ORDER BY
22+
scroll_count DESC,
23+
session_id ASC;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g3601_3700.s3674_minimum_operations_to_equalize_array;
2+
3+
// #Easy #Weekly_Contest_466 #2025_09_07_Time_1_ms_(100.00%)_Space_43.98_MB_(100.00%)
4+
5+
public class Solution {
6+
public int minOperations(int[] nums) {
7+
for (int num : nums) {
8+
if (num != nums[0]) {
9+
return 1;
10+
}
11+
}
12+
return 0;
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3674\. Minimum Operations to Equalize Array
2+
3+
Easy
4+
5+
You are given an integer array `nums` of length `n`.
6+
7+
In one operation, choose any subarray `nums[l...r]` (`0 <= l <= r < n`) and **replace** each element in that subarray with the **bitwise AND** of all elements.
8+
9+
Return the **minimum** number of operations required to make all elements of `nums` equal.
10+
11+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2]
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
Choose `nums[0...1]`: `(1 AND 2) = 0`, so the array becomes `[0, 0]` and all elements are equal in 1 operation.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [5,5,5]
26+
27+
**Output:** 0
28+
29+
**Explanation:**
30+
31+
`nums` is `[5, 5, 5]` which already has all elements equal, so 0 operations are required.
32+
33+
**Constraints:**
34+
35+
* `1 <= n == nums.length <= 100`
36+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3601_3700.s3675_minimum_operations_to_transform_string;
2+
3+
// #Medium #Weekly_Contest_466 #2025_09_14_Time_5_ms_(100.00%)_Space_47.93_MB_(95.06%)
4+
5+
public class Solution {
6+
public int minOperations(String s) {
7+
int n = s.length();
8+
int ans = 0;
9+
for (int i = 0; i < n; i++) {
10+
final char c = s.charAt(i);
11+
if (c != 'a') {
12+
int ops = 'z' - c + 1;
13+
if (ops > ans) {
14+
ans = ops;
15+
}
16+
if (ops == 25) {
17+
break;
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3675\. Minimum Operations to Transform String
2+
3+
Medium
4+
5+
You are given a string `s` consisting only of lowercase English letters.
6+
7+
You can perform the following operation any number of times (including zero):
8+
9+
* Choose any character `c` in the string and replace **every** occurrence of `c` with the **next** lowercase letter in the English alphabet.
10+
11+
12+
Return the **minimum** number of operations required to transform `s` into a string consisting of **only** `'a'` characters.
13+
14+
**Note:** Consider the alphabet as circular, thus `'a'` comes after `'z'`.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "yz"
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
* Change `'y'` to `'z'` to get `"zz"`.
25+
* Change `'z'` to `'a'` to get `"aa"`.
26+
* Thus, the answer is 2.
27+
28+
**Example 2:**
29+
30+
**Input:** s = "a"
31+
32+
**Output:** 0
33+
34+
**Explanation:**
35+
36+
* The string `"a"` only consists of `'a'` characters. Thus, the answer is 0.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= s.length <= 5 * 10<sup>5</sup></code>
41+
* `s` consists only of lowercase English letters.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3601_3700.s3676_count_bowl_subarrays;
2+
3+
// #Medium #Weekly_Contest_466 #2025_09_14_Time_2_ms_(100.00%)_Space_58.91_MB_(69.85%)
4+
5+
public class Solution {
6+
public long bowlSubarrays(int[] nums) {
7+
int n = nums.length;
8+
int res = n;
9+
int pre = 0;
10+
for (int a : nums) {
11+
if (a > pre) {
12+
res--;
13+
pre = a;
14+
}
15+
}
16+
pre = 0;
17+
for (int i = n - 1; i >= 0; i--) {
18+
if (nums[i] > pre) {
19+
res--;
20+
pre = nums[i];
21+
}
22+
}
23+
return res + 1L;
24+
}
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3676\. Count Bowl Subarrays
2+
3+
Medium
4+
5+
You are given an integer array `nums` with **distinct** elements.
6+
7+
A subarray `nums[l...r]` of `nums` is called a **bowl** if:
8+
9+
* The subarray has length at least 3. That is, `r - l + 1 >= 3`.
10+
* The **minimum** of its two ends is **strictly greater** than the **maximum** of all elements in between. That is, `min(nums[l], nums[r]) > max(nums[l + 1], ..., nums[r - 1])`.
11+
12+
Return the number of **bowl** subarrays in `nums`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,5,3,1,4]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
The bowl subarrays are `[3, 1, 4]` and `[5, 3, 1, 4]`.
23+
24+
* `[3, 1, 4]` is a bowl because `min(3, 4) = 3 > max(1) = 1`.
25+
* `[5, 3, 1, 4]` is a bowl because `min(5, 4) = 4 > max(3, 1) = 3`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [5,1,2,3,4]
30+
31+
**Output:** 3
32+
33+
**Explanation:**
34+
35+
The bowl subarrays are `[5, 1, 2]`, `[5, 1, 2, 3]` and `[5, 1, 2, 3, 4]`.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [1000000000,999999999,999999998]
40+
41+
**Output:** 0
42+
43+
**Explanation:**
44+
45+
No subarray is a bowl.
46+
47+
**Constraints:**
48+
49+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
51+
* `nums` consists of distinct elements.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3601_3700.s3677_count_binary_palindromic_numbers;
2+
3+
// #Hard #Weekly_Contest_466 #2025_09_07_Time_1_ms_(100.00%)_Space_40.86_MB_(100.00%)
4+
5+
public class Solution {
6+
private long makePalin(long left, boolean odd) {
7+
long ans = left;
8+
if (odd) {
9+
left = left >> 1;
10+
}
11+
while (left > 0) {
12+
ans = (ans << 1) | (left & 1);
13+
left = left >> 1;
14+
}
15+
return ans;
16+
}
17+
18+
public int countBinaryPalindromes(long n) {
19+
if (n == 0) {
20+
return 1;
21+
}
22+
int len = 64 - Long.numberOfLeadingZeros(n);
23+
long count = 1;
24+
for (int i = 1; i < len; i++) {
25+
int half = (i + 1) / 2;
26+
count += 1L << (half - 1);
27+
}
28+
int half = (len + 1) / 2;
29+
long prefix = n >> (len - half);
30+
long palin = makePalin(prefix, len % 2 == 1);
31+
count += (prefix - (1L << (half - 1)));
32+
if (palin <= n) {
33+
++count;
34+
}
35+
return (int) count;
36+
}
37+
}

0 commit comments

Comments
 (0)