319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.
Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not engage in multiple transactions simultaneously.
Example 1:
Input: prices = [1,3,2,8,4,9], fee = 2
Output: 8
Explanation: Buy day 1 (price=1), sell day 4 (price=8), profit=8-1-2=5.
Buy day 5 (price=4), sell day 6 (price=9), profit=9-4-2=3. Total=8.
Example 2:
Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6
Explanation: Buy day 1, sell day 5, profit=10-1-3=6.
1 <= prices.length <= 5 * 10^41 <= prices[i] < 5 * 10^40 <= fee < 5 * 10^4The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.
arr = [2,3,4], the median is 3.arr = [2,3], the median is (2 + 3) / 2 = 2.5.Implement the MedianFinder class:
constructor() initializes the MedianFinder object.addNum(int num) adds the integer num from the data stream to the data structure.findMedian() returns the median of all elements so far. Answers within 10^-5 of the actual answer will be accepted.Example 1:
Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]
Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
-10^5 <= num <= 10^5findMedian.5 * 10^4 calls will be made to addNum and findMedian.You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price=1), sell on day 3 (price=5), profit=4.
Then buy on day 4 (price=3), sell on day 5 (price=6), profit=3.
Total profit = 4 + 3 = 7.
Example 2:
Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1, sell on day 5, profit=4. (Or buy/sell each consecutive day.)
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: No profitable transaction possible.
1 <= prices.length <= 3 * 10^40 <= prices[i] <= 10^4Write a solution to find the daily active user count for a period of 30 days ending 2019-07-27 inclusively. A user was active on someday if they made at least one activity on that day.
Return the result table in any order.
| Column Name | Type | | :--- | :--- | | user_id | int | | session_id | int | | activity_date | date | | activity_type | enum |
There is no primary key for this table (it may have duplicate rows).
The activity_type column is an ENUM of type ('open_session', 'end_session', 'scroll_down', 'send_message').
The table has information about the activity of users in a social media website.
Input: Activity table: | user_id | session_id | activity_date | activity_type | | :--- | :--- | :--- | :--- | | 1 | 1 | 2019-07-20 | open_session | | 1 | 1 | 2019-07-20 | scroll_down | | 1 | 1 | 2019-07-20 | end_session | | 2 | 4 | 2019-07-20 | open_session | | 2 | 4 | 2019-07-21 | send_message | | 2 | 4 | 2019-07-21 | end_session | | 3 | 2 | 2019-07-21 | open_session | | 3 | 2 | 2019-07-21 | send_message | | 3 | 2 | 2019-07-21 | end_session | | 4 | 3 | 2019-06-25 | open_session |
Output: | day | active_users | | :--- | :--- | | 2019-07-20 | 2 | | 2019-07-21 | 2 |
Explanation: Note that we do not care about the days with zero active users.
Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
Example 1:
Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted.
Example 2:
Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4nums is sorted in strictly increasing order.There are n cars going to the same destination along a one-lane road. The destination is target miles away.
You are given two integer arrays position and speed, both of length n, where position[i] is the position of the ith car and speed[i] is the speed of the ith car (in miles per hour).
A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper at the same speed. The faster car will slow down to match the slower car's speed. The distance between these two cars is ignored (they are assumed to be at the same position).
A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.
If a car catches up to a car fleet right at the destination point, it still counts as one fleet.
Return the number of car fleets that will arrive at the destination.
Example 1:
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Explanation:
- Car at position 10 with speed 2: arrives at time (12-10)/2 = 1
- Car at position 8 with speed 4: arrives at time (12-8)/4 = 1. Catches car at 10, forms fleet.
- Car at position 5 with speed 1: arrives at time (12-5)/1 = 7. Alone.
- Car at position 3 with speed 3: arrives at time (12-3)/3 = 3. Catches car at 5? No, 3 < 7, arrives before. Alone.
- Car at position 0 with speed 1: arrives at time (12-0)/1 = 12. Alone.
3 fleets: {10,8}, {5}, {3}, but car 0 catches fleet with car 5? Actually need to process closest to target first.
Example 2:
Input: target = 10, position = [3], speed = [3]
Output: 1
Example 3:
Input: target = 100, position = [0,2,4], speed = [4,2,1]
Output: 1
Explanation: All cars eventually merge into one fleet.
n == position.length == speed.lengthYou are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Example 3:
Input: n = 5
Output: 8
Explanation: The 8 distinct ways are:
1+1+1+1+1, 1+1+1+2, 1+1+2+1, 1+2+1+1, 2+1+1+1, 2+2+1, 2+1+2, 1+2+2
1 <= n <= 45Can you solve this in O(1) space?
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4nums contains distinct values sorted in ascending order.-10^4 <= target <= 10^4Implement a complete JWT (JSON Web Token) authentication system for a Node.js/Express API. The system should handle user registration, login, token verification, token refresh, and protecting routes with authentication middleware.
| Method | Endpoint | Auth Required | Description |
|--------|----------|---------------|-------------|
| POST | /auth/register | No | Register a new user |
| POST | /auth/login | No | Login and get tokens |
| POST | /auth/refresh | No | Refresh access token |
| POST | /auth/logout | Yes | Invalidate refresh token |
| GET | /api/profile | Yes | Get user profile (protected) |
Access Token Payload:
{
userId: string;
email: string;
role: string;
iat: number; // issued at
exp: number; // expires at (1 hour)
}
Refresh Token Payload:
{
userId: string;
tokenId: string; // unique ID for revocation
iat: number;
exp: number; // expires at (7 days)
}
Input:
POST /auth/register
{ "email": "alice@example.com", "password": "Str0ng!Pass", "name": "Alice" }
Output (201):
{
"user": { "id": "uuid-1", "email": "alice@example.com", "name": "Alice" },
"accessToken": "eyJhbGciOiJIUzI1NiIs...",
"refreshToken": "eyJhbGciOiJIUzI1NiIs...",
"expiresIn": 3600
}
Write a SQL query to compute the moving average of how much the customer paid in a seven-day window (current day + 6 days before). average_amount should be rounded to two decimal places.
Return the result table ordered by visited_on in ascending order.
The query result format is in the following example.
Table: Customer
| Column Name | Type |
| :--- | :--- |
| customer_id | int |
| name | varchar |
| visited_on | date |
| amount | int |
(customer_id, visited_on) is the primary key for this table.
This table contains data about customer transactions in a restaurant.
Input:
Customer table:
| customer_id | name | visited_on | amount |
| :--- | :--- | :--- | :--- |
| 1 | Jhon | 2019-01-01 | 100 |
| 2 | Daniel | 2019-01-02 | 110 |
| 3 | Jade | 2019-01-03 | 120 |
| 4 | Khaled | 2019-01-04 | 130 |
| 5 | Winston | 2019-01-05 | 110 |
| 6 | Elvis | 2019-01-06 | 140 |
| 7 | Anna | 2019-01-07 | 150 |
| 8 | Maria | 2019-01-08 | 80 |
| 9 | Jaze | 2019-01-09 | 110 |
| 1 | Jhon | 2019-01-10 | 130 |
Output: | visited_on | amount | average_amount | | :--- | :--- | :--- | | 2019-01-07 | 860 | 122.86 | | 2019-01-08 | 840 | 120 | | 2019-01-09 | 840 | 120 | | 2019-01-10 | 850 | 121.43 |
Explanation: 1st moving average from 2019-01-01 to 2019-01-07 has sum = 100 + 110 + 120 + 130 + 110 + 140 + 150 = 860. Average = 860 / 7 = 122.86. 2nd moving average from 2019-01-02 to 2019-01-08 has sum = 110 + 120 + 130 + 110 + 140 + 150 + 80 = 840. Average = 840 / 7 = 120. ... and so on.
Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.
The test cases are generated so that the answer will fit in a 32-bit integer.
A subarray is a contiguous subsequence of the array.
Example 1:
Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a contiguous subarray.
1 <= nums.length <= 2 * 10^4-10 <= nums[i] <= 10nums is guaranteed to fit in a 32-bit integer.Given two integers a and b, return the sum of the two integers without using the operators + and -.
Input: a = 1, b = 2
Output: 3
Input: a = 2, b = 3
Output: 5
-1000 <= a, b <= 10001 <= n <= 10^50 < target <= 10^60 <= position[i] < target0 < speed[i] <= 10^6Input:
POST /auth/login
{ "email": "alice@example.com", "password": "Str0ng!Pass" }
Output (200):
{
"accessToken": "eyJhbGciOiJIUzI1NiIs...",
"refreshToken": "eyJhbGciOiJIUzI1NiIs...",
"expiresIn": 3600
}
Input:
GET /api/profile
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Output (200):
{
"id": "uuid-1",
"email": "alice@example.com",
"name": "Alice"
}
Input:
GET /api/profile
Authorization: Bearer <expired-token>
Output (401):
{
"error": "Token expired",
"statusCode": 401
}