Yükleniyor...
Tüm platformlardaki algoritma soruları ve çözümleri. Filtreleyerek aradığını bul!
Platforma Göre:
Dile Göre:
Zorluğa Göre:
Soru Ara:
Add Binary Given two binary strings a and b, return their sum as a binary string. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010", b = "1011" Output: "10101" Constraints: 1 <= a.length, b.length <= 104 a and b consist only of '0' or '1' characters. Each string does not contain leading zeros except for the zero itself. * @param {string} a @param {string} b @return {string}
Apply Transform Over Each Element in Array Given an integer array arr and a mapping function fn, return a new array with a transformation applied to each element. The returned array should be created such that returnedArray[i] = fn(arr[i], i). Please solve it without the built-in Array.map method. Example 1: Input: arr = [1,2,3], fn = function plusone(n) { return n + 1; } Output: [2,3,4] Explanation: const newArray = map(arr, plusone); // [2,3,4] The function increases each value in the array by one. Example 2: Input: arr = [1,2,3], fn = function plusI(n, i) { return n + i; } Output: [1,3,5] Explanation: The function increases each value by the index it resides in. Example 3: Input: arr = [10,20,30], fn = function constant() { return 42; } Output: [42,42,42] Explanation: The function always returns 42. Constraints: 0 <= arr.length <= 1000 -109 <= arr[i] <= 109 fn returns a number * @param {number[]} arr @param {Function} fn @return {number[]}
Counter Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc). Example 1: Input: n = 10 ["call","call","call"] Output: [10,11,12] Explanation: counter() = 10 // The first time counter() is called, it returns n. counter() = 11 // Returns 1 more than the previous time. counter() = 12 // Returns 1 more than the previous time. Example 2: Input: n = -2 ["call","call","call","call","call"] Output: [-2,-1,0,1,2] Explanation: counter() initially returns -2. Then increases after each sebsequent call. Constraints: -1000 <= n <= 1000 0 <= calls.length <= 1000 calls[i] === "call" * @param {number} n @return {Function} counter * const counter = createCounter(10) counter() // 10 counter() // 11 counter() // 12
Counter II Write a function createCounter. It should accept an initial integer init. It should return an object with three functions. The three functions are: increment() increases the current value by 1 and then returns it. decrement() reduces the current value by 1 and then returns it. reset() sets the current value to init and then returns it. Example 1: Input: init = 5, calls = ["increment","reset","decrement"] Output: [6,5,4] Explanation: const counter = createCounter(5); counter.increment(); // 6 counter.reset(); // 5 counter.decrement(); // 4 Example 2: Input: init = 0, calls = ["increment","increment","decrement","reset","reset"] Output: [1,2,1,0,0] Explanation: const counter = createCounter(0); counter.increment(); // 1 counter.increment(); // 2 counter.decrement(); // 1 counter.reset(); // 0 counter.reset(); // 0 Constraints: -1000 <= init <= 1000 0 <= calls.length <= 1000 calls[i] is one of "increment", "decrement" and "reset" * @param {integer} init @return { increment: Function, decrement: Function, reset: Function } * const counter = createCounter(5) counter.increment(); // 6 counter.reset(); // 5 counter.decrement(); // 4
Create Hello World Function Write a function createHelloWorld. It should return a new function that always returns "Hello World". Example 1: Input: args = [] Output: "Hello World" Explanation: const f = createHelloWorld(); f(); // "Hello World" The function returned by createHelloWorld should always return "Hello World". Example 2: Input: args = [{},null,42] Output: "Hello World" Explanation: const f = createHelloWorld(); f({}, null, 42); // "Hello World" Any arguments could be passed to the function but it should still always return "Hello World". Constraints: 0 <= args.length <= 10 * @return {Function}
Find the Index of the First Occurrence in a String Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "sadbutsad", needle = "sad" Output: 0 Explanation: "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. Example 2: Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1. Constraints: 1 <= haystack.length, needle.length <= 104 haystack and needle consist of only lowercase English characters. * @param {string} haystack @param {string} needle @return {number}
Length of Last Word Given a string s consisting of words and spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. Example 1: Input: s = "Hello World" Output: 5 Explanation: The last word is "World" with length 5. Example 2: Input: s = " fly me to the moon " Output: 4 Explanation: The last word is "moon" with length 4. Example 3: Input: s = "luffy is still joyboy" Output: 6 Explanation: The last word is "joyboy" with length 6. Constraints: 1 <= s.length <= 104 s consists of only English letters and spaces ' '. There will be at least one word in s. * @param {string} s @return {number}
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Constraints: 1 <= strs.length <= 200 0 <= strs[i].length <= 200 strs[i] consists of only lowercase English letters. * @param {string[]} strs @return {string}
* @param {number[]} nums1 @param {number} m @param {number[]} nums2 @param {number} n @return {void} Do not return anything, modify nums1 in-place instead.
Merge Two Sorted Lists You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: list1 = [], list2 = [] Output: [] Example 3: Input: list1 = [], list2 = [0] Output: [0] Constraints: The number of nodes in both lists is in the range [0, 50]. -100 <= Node.val <= 100 Both list1 and list2 are sorted in non-decreasing order.
Palindrome Number Given an integer x, return true if x is a palindrome, and false otherwise. Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2: 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. Example 3: Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Constraints: -231 <= x <= 231 - 1 * @param {number} x @return {boolean}
Plus One You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. Increment the large integer by one and return the resulting array of digits. Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4]. Example 2: Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2]. Example 3: Input: digits = [9] Output: [1,0] Explanation: The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0]. Constraints: 1 <= digits.length <= 100 0 <= digits[i] <= 9 digits does not contain any leading 0's.
Remove Duplicates from Sorted Array Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums. Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things: Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums. Return k. Custom Judge: The judge will test your solution with the following code: int[] nums = [...]; // Input array int[] expectedNums = [...]; // The expected answer with correct length int k = removeDuplicates(nums); // Calls your implementation assert k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] == expectedNums[i]; } If all assertions pass, then your solution will be accepted. Example 1: Input: nums = [1,1,2] Output: 2, nums = [1,2,_] Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). Example 2: Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). Constraints: 1 <= nums.length <= 3 * 104 -100 <= nums[i] <= 100 nums is sorted in non-decreasing order. * @param {number[]} nums @return {number}
Remove Duplicates from Sorted List Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1: Input: head = [1,1,2] Output: [1,2] Example 2: Input: head = [1,1,2,3,3] Output: [1,2,3] Constraints: The number of nodes in the list is in the range [0, 300]. -100 <= Node.val <= 100 The list is guaranteed to be sorted in ascending order. * Definition for singly-linked list. function ListNode(val, next) { this.val = (val===undefined ? 0 : val) this.next = (next===undefined ? null : next) } * @param {ListNode} head @return {ListNode}
Remove Element Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val. Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things: Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums. Return k. Custom Judge: The judge will test your solution with the following code: int[] nums = [...]; // Input array int val = ...; // Value to remove int[] expectedNums = [...]; // The expected answer with correct length. // It is sorted with no values equaling val. int k = removeElement(nums, val); // Calls your implementation assert k == expectedNums.length; sort(nums, 0, k); // Sort the first k elements of nums for (int i = 0; i < actualLength; i++) { assert nums[i] == expectedNums[i]; } If all assertions pass, then your solution will be accepted. Example 1: Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Explanation: Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores). Example 2: Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores). Constraints: 0 <= nums.length <= 100 0 <= nums[i] <= 50 0 <= val <= 100 * @param {number[]} nums @param {number} val @return {number}
Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. 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 a roman numeral, convert it to an integer. Example 1: Input: s = "III" Output: 3 Explanation: III = 3. Example 2: Input: s = "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 3: Input: s = "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. Constraints: 1 <= s.length <= 15 s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M'). It is guaranteed that s is a valid roman numeral in the range [1, 3999]. * @param {string} s @return {number}
Search Insert Position 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 Constraints: 1 <= nums.length <= 104 -104 <= nums[i] <= 104 nums contains distinct values sorted in ascending order. -104 <= target <= 104 * @param {number[]} nums @param {number} target @return {number}
Sqrt(x) Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator. For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Example 1: Input: x = 4 Output: 2 Explanation: The square root of 4 is 2, so we return 2. Example 2: Input: x = 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned. Constraints: 0 <= x <= 231 - 1 * @param {number} x @return {number}
To Be Or Not To Be Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functions. toBe(val) accepts another value and returns true if the two values === each other. If they are not equal, it should throw an error "Not Equal". notToBe(val) accepts another value and returns true if the two values !== each other. If they are equal, it should throw an error "Equal". Example 1: Input: func = () => expect(5).toBe(5) Output: {"value": true} Explanation: 5 === 5 so this expression returns true. Example 2: Input: func = () => expect(5).toBe(null) Output: {"error": "Not Equal"} Explanation: 5 !== null so this expression throw the error "Not Equal". Example 3: Input: func = () => expect(5).notToBe(null) Output: {"value": true} Explanation: 5 !== null so this expression returns true. * @param {string} val @return {Object} * expect(5).toBe(5); // true expect(5).notToBe(5); // throws "Equal"
Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1] Constraints: 2 <= nums.length <= 104 -109 <= nums[i] <= 109 -109 <= target <= 109 Only one valid answer exists. * @param {number[]} nums @param {number} target @return {number[]}
Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false Constraints: 1 <= s.length <= 104 s consists of parentheses only '()[]{}'. Time complexity: O(n) Space complexity: O(n)
HackerRank - Birthday Cake Candles You are in charge of the cake for a child's birthday. Count how many candles are tallest — only those can be blown out.
HackerRank - Mini-Max Sum Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Print the respective minimum and maximum values on a single line. min sum = exclude the largest element, max sum = exclude the smallest
HackerRank - Plus Minus Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal.
HackerRank - Staircase Print a staircase of height n. The staircase is right-aligned, composed of # symbols and spaces. The last line is not preceded by any spaces.
HackerRank - Time Conversion Given a time in 12-hour AM/PM format, convert it to 24-hour (military) time. Examples: 12:00:00AM -> 00:00:00 12:00:00PM -> 12:00:00 07:05:45PM -> 19:05:45
HackerRank - A Very Big Sum Calculate and print the sum of array elements, considering that some values can be very large. Python handles arbitrarily large integers natively.
HackerRank - Simple Array Sum Given an array of integers, find the sum of its elements.
HackerRank - Solve Me First Compute the sum of two integers.
CodeWars - Adults only (SQL for Beginners #1) From the users table (name, age), return all users who are 18 or older.
CodeWars - Best-Selling Books (SQL for Beginners #5) From the books table (name, author, copies_sold), return the top 5 best-selling books ordered by copies sold (highest to lowest).
CodeWars - Easy SQL: Bit Length Given a demographics table (id, name, birthday, race), return the same table where all text fields (name & race) are replaced with the bit length of the string.
CodeWars - Collect Tuition (SQL for Beginners #4) From the students table (name, age, semester, mentor, tuition_received), return all students who have NOT yet paid their tuition.
CodeWars - Countries Capitals for Trivia Night (SQL for Beginners #6) From the countries table (country, capital, continent), return the capitals of African countries whose name starts with 'E', ordered alphabetically. Return at most 3 results. Note: continent may be stored as 'Africa' or 'Afrika'.
CodeWars - On the Canadian Border (SQL for Beginners #2) From the travelers table (name, country), return travelers who need a visa check — i.e. exclude citizens of USA, Canada, and Mexico.
CodeWars - SQL with Pokemon: Damage Multipliers You are battling Erika at the Celadon Gym (Grass-type). Using the pokemon (id, pokemon_name, element_id, str) and multipliers (id, element, multiplier) tables, return pokemon whose modifiedStrength (multiplier * str) >= 40, ordered from strongest to weakest. Output: pokemon_name, modifiedStrength, element i choose you! --
CodeWars - Register for the Party (SQL for Beginners #3) Insert yourself into the participants table (name, age, attending). Only attendees aged 21+ are allowed since alcohol is served.
CodeWars - SQL Basics: Repeat and Reverse Using the monsters table (id, name, legs, arms, characteristics), return name repeated three times and characteristics reversed. Output: name, characteristics
CodeWars - SQL Basics: Simple IN Using departments (id, name) and sales (id, department_id, name, price, card_name, card_number, transaction_date), return departments that have had at least one sale with a price over $98. Output: id, name
CodeWars - Third Angle of a Triangle Given a table 'otherangle' with columns 'a' and 'b' (two interior angles), return a, b and the third angle as 'res'. Sum of angles in a triangle = 180 degrees.
CodeWars - Top 10 customers by total payments Using the DVD Rental database, return the top 10 customers ordered by total amount spent (highest to lowest). Output columns: customer_id [int] email [varchar] payments_count [int] total_amount [float]
Write a function createHelloWorld. It should return a new function that always returns "Hello World". Example 1: Input: args = [] Output: "Hello World" Explanation: const f = createHelloWorld(); f(); // "Hello World" The function returned by createHelloWorld should always return "Hello World". Example 2: Input: args = [{},null,42] Output: "Hello World" Explanation: const f = createHelloWorld(); f({}, null, 42); // "Hello World" Any arguments could be passed to the function but it should still always return "Hello World". Constraints: 0 <= args.length <= 10
Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc). Example 1: Input: n = 10 ["call","call","call"] Output: [10,11,12] Explanation: counter() = 10 // The first time counter() is called, it returns n. counter() = 11 // Returns 1 more than the previous time. counter() = 12 // Returns 1 more than the previous time. Example 2: Input: n = -2 ["call","call","call","call","call"] Output: [-2,-1,0,1,2] Explanation: counter() initially returns -2. Then increases after each sebsequent call. Constraints: -1000 <= n <= 1000 0 <= calls.length <= 1000 calls[i] === "call"