319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Given an integer x, return true if x is a palindrome, and false otherwise.
An integer is a palindrome when it reads the same forward and backward. For example, 121 is a palindrome while 123 is not.
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
-2^31 <= x <= 2^31 - 1Follow up: Could you solve it without converting the integer to a string?
Given a string s, return the number of palindromic substrings in it.
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
1 <= s.length <= 1000s consists of lowercase English letters.Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [2,1], p = 2, q = 1
Output: 2
[2, 10^5].-10^9 <= Node.val <= 10^9Node.val are unique.p != qp and q will exist in the BST.Implement a multithreaded FizzBuzz using four concurrent functions:
fizz() - called for numbers divisible by 3 (but not 15)buzz() - called for numbers divisible by 5 (but not 15)fizzbuzz() - called for numbers divisible by 15number() - called for all other numbersAll four functions run concurrently and must coordinate to produce the correct FizzBuzz sequence from 1 to n.
class FizzBuzzMT {
constructor(n: number);
fizz(printFizz: () => void): Promise<void>;
buzz(printBuzz: () => void): Promise<void>;
fizzbuzz(printFizzBuzz: () => void): Promise<void>;
number(printNumber: (n: number) => void): Promise<void>;
}
Input: n = 15
Output: ["1","2","fizz","4","buzz","fizz","7","8","fizz","buzz","11","fizz","13","14","fizzbuzz"]
Seven different symbols represent Roman numerals: I, V, X, L, C, D and M.
| Symbol | Value | | :--- | :--- | | I | 1 | | V | 5 | | X | 10 | | L | 50 | | C | 100 | | D | 500 | | M | 1000 |
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.X can be placed before L (50) and C (100) to make 40 and 90.C can be placed before D (500) and M (1000) to make 400 and 900.Given an integer, convert it to a Roman numeral.
Input: num = 3749
Output: "MMMDCCXLIX"
Explanation:
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
1 <= num <= 3999You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure as shown in the example below.
Given the head of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Let curr be a node with a child list. The nodes in the child list should appear after curr and before curr.next in the flattened list.
Return the head of the flattened list. The nodes in the list must have all of their child pointers set to null.
Example 1:
Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
Explanation: The multilevel linked list in the input is shown.
After flattening the multilevel linked list it becomes:
1 -> 2 -> 3 -> 7 -> 8 -> 11 -> 12 -> 9 -> 10 -> 4 -> 5 -> 6
Example 2:
Input: head = [1,2,null,3]
Output: [1,3,2]
Explanation: The multilevel linked list in the input is shown.
After flattening the multilevel linked list it becomes:
1 -> 3 -> 2
Example 3:
Input: head = []
Output: []
1000.1 <= Node.val <= 10^5Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat counts, k. For example, there will not be input like 3a or 2[4].
The test cases are generated so that the length of the output will never exceed 10^5.
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Input: s = "3[a2[c]]"
Output: "accaccacc"
Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"
1 <= s.length <= 30s consists of lowercase English letters, digits, and square brackets '[]'.s is guaranteed to be a valid input.s are in the range [1, 300].Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
0 <= n <= 10^5Follow up:
O(n log n). Can you do it in linear time O(n) and possibly in a single pass?__builtin_popcount in C++)?Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
Can you solve it without sorting?
Example 1:
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
Example 2:
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
1 <= k <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4In this challenge, you are acting as a Security Engineer performing a code review. Below is a snippet of a Node.js/Express application that handles user profiles and search.
The code contains exactly 4 major security vulnerabilities (OWASP Top 10). Your task is to identify them.
1: const express = require('express');
2: const router = express.Router();
3: const db = require('./database');
4:
5: router.get('/profile', async (req, res) => {
6: const user = await db.query(`SELECT * FROM users WHERE id = ${req.query.id}`);
7: res.send(`<h1>Welcome, ${user.username}</h1>`);
8: });
9:
10: router.get('/debug-fetch', async (req, res) => {
11: const response = await fetch(req.query.url);
12: const data = await response.json();
13: res.json(data);
14: });
15:
16: router.post('/update-email', async (req, res) => {
17: if (req.session.isLoggedIn) {
18: await db.query('UPDATE users SET email = ? WHERE id = ?', [req.body.email, req.body.id]);
19: res.send('Email updated!');
20: }
21: });
Identify the vulnerability type and the line number for each of the 4 flaws.
[
{ "line": 5, "type": "SQL Injection", "reason": "..." },
...
]
CREATE TABLE Employee (
id INT PRIMARY KEY,
name VARCHAR(255),
salary INT,
managerId INT REFERENCES Employee(id)
);
Each row indicates the ID, name, salary, and manager of an employee. If managerId is NULL, the employee does not have a manager.
Write a SQL query to find the employees who earn more than their managers.
Return the result table with the column name Employee.
Input:
Employee table: | id | name | salary | managerId | |----|-------|--------|-----------| | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL |
Output:
| Employee | |----------| | Joe |
Explanation: Joe earns 70000 and his manager Sam earns 60000, so Joe earns more than his manager. Henry earns 80000 but his manager Max earns 90000.
Input:
Employee table: | id | name | salary | managerId | |----|-------|--------|-----------| | 1 | Alice | 50000 | 2 | | 2 | Bob | 60000 | NULL |
Output:
| Employee | |----------|
(Empty result - Alice earns less than her manager Bob.)
Build a real-time notification system using WebSockets. The system should handle user connections, room-based subscriptions, targeted and broadcast notifications, presence tracking, and reliable message delivery with acknowledgements.
| Event | Direction | Description |
|-------|-----------|-------------|
| connect | Client -> Server | Authenticate and establish connection |
| subscribe | Client -> Server | Join a notification room |
| unsubscribe | Client -> Server | Leave a notification room |
| notification | Server -> Client | Deliver a notification |
| ack | Client -> Server | Acknowledge receipt |
| presence | Server -> Client | Online status updates |
interface Notification {
id: string;
type: string;
title: string;
body: string;
data?: Record<string, any>;
timestamp: string;
read: boolean;
}
interface ConnectionInfo {
userId: string;
socketId: string;
rooms: Set<string>;
connectedAt: string;
}
Client: connect({ token: 'jwt-token-for-user1' })
Server: { event: 'connected', userId: 'user1', socketId: 'abc123' }
Client: subscribe({ room: 'order-updates' })
Server: { event: 'subscribed', room: 'order-updates' }
Server sends to user1:
{
id: 'notif-1',
type: 'order-shipped',
title: 'Order Shipped',
body: 'Your order #123 has been shipped',
data: { orderId: 123, trackingNumber: 'XYZ' },
timestamp: '2024-01-15T10:30:00Z'
}
Client: ack({ notificationId: 'notif-1' })
broadcast('order-updates', {
type: 'flash-sale',
title: 'Flash Sale!',
body: '50% off all items for the next hour'
})
// All users subscribed to 'order-updates' receive this notification