Join the Peerlist 30 Days LeetCode Challenge
We are hosting a 30-day LeetCode Challenge on Peerlist! If you want to hone your problem-solving skills, join us and learn from the community.
Yogini Bende
Oct 03, 2024 • 14 min read
Job hunting season is on, and we know how much preparation goes into it. Many of us love solving LeetCode problems to enhance our problem-solving skills, improve our understanding of data structures and algorithms, and prepare for technical interviews.
That’s why we’re bringing you a 30-day LeetCode Challenge!
By dedicating just 30 minutes to an hour each day to solving LeetCode problems, you can make significant progress in a short time. But doing this alone can be challenging, so we’re adding a little accountability with this.
Let’s take this challenge together as a community. By the end of 30 days, you'll have plenty of new learnings.
Starting today, we’re launching the LeetCode Challenge—a 30-day journey for all developers who want to improve their problem-solving skills.
How It Works:
Daily Problem Statement: Every evening, we will share one LeetCode problem statement through this blog. Your task is to solve it using any programming language of your choice!
Post Your Submissions: Once you’ve solved the problem, post your submissions as a GitHub repository or a CodeSandbox link along with your learnings of the day on Peerlist under the context #LeetCodeChallenge.
Learn and Share Feedback: Engage with fellow developers by sharing feedback and learning from each other.
How to Submit Your Submission?
Open Peerlist Scroll
Click on "Create New Post" and select #LeetCodeChallenge from the context.
Submit your solution and learnings as a post. Include the Challenge Name and Day of the challenge. If you've added the code to GitHub, Codepen, or Figma, include those links.
Take a screenshot and share it along with your post. You can also add gifs.
Add any learnings from the task (optional).
Hit post! Your submission for the day is done.
So, are you ready? For the week one, we will be focusing on arrays and strings! Let's start with an easy one. Here is your Day 1 challenge!
Challenge 1 — 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.
Read detailed problem statement here.
Challenge 2 — Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example:
Input: prices = [7,1,5,3,6,4]
Output: 5
Read detailed problem statement here.
Challenge 3 — Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Read detailed problem statement here.
Challenge 4 — Product of Array Except Self
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums of length 1 is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation.
Example:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Read detailed problem statement here.
Challenge 5 — Find All Anagrams in a String
Given two strings s and p, return all the start indices of p's anagrams in s. The answer can be in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example:
Input: s = "cbaebabacd", p = "abc"
Output: [0,6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substring with start index = 6 is "bac", which is an anagram of "abc".
Read detailed problem statement here.
Challenge 6 — Group Anagrams
Problem: Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Read detailed problem statement here.
Challenge 7 — Sort Colors
Problem: Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
Example:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Read detailed problem statement here.
Challenge 8 — Reverse Linked List
Problem: Given the head of a singly linked list, reverse the list and return the reversed list.
Example:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Read detailed problem statement here.
Challenge 9 — Merge Two Sorted Lists
Problem: Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]
Read detailed problem statement here.
Challenge 10 — Middle of the Linked List
Problem: Given the head of a linked list, return the middle node. If there are two middle nodes, return the second middle node.
Example:
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Read detailed problem statement here.
Challenge 11 — Remove Nth Node From End of List
Problem: Given the head of a linked list, remove the nth node from the end of the list and return its head.
Example:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Read detailed problem statement here.
Challenge 12 — Add Two Numbers
Problem: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return it as a linked list.
Example:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Read detailed problem statement here.
Challenge 13 — Min Stack
Problem: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class.
Example:
Input:
minStack = MinStack()
minStack.push(-2)
minStack.push(0)
minStack.push(-3)
minStack.getMin()
→returns -3
minStack.pop()
minStack.top()
→returns 0
minStack.getMin()
→returns -2
Read detailed problem statement here.
Challenge 14 — Valid Parenthesis
Problem: Given a string 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.
Example:
Input: s = "()"
Output: true
Read detailed problem statement here.
Challenge 15 — Number of Islands
Problem: Given an m x n 2D binary grid grid which represents a map where 1s (land) and 0s (water) are represented, return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
Example:
Input:
grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
Read detailed problem statement here.
Challenge 16 — Binary Tree Level Order Traversal
Problem: Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
Example:
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Read detailed problem statement here.
Challenge 17 — Lowest Common Ancestor of a Binary Tree
Problem: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 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 the tree that has both p and q as descendants."
Example:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Read detailed problem statement here.
Challenge 18 — Construct Binary Search Tree from Preorder Traversal
Problem: Given an array of integers preorder where preorder[i] is the value of the ith node visited in the preorder traversal of a BST (binary search tree), construct the BST from this traversal.
Example:
Input: preorder = [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]
Read detailed problem statement here.
Challenge 19 — Search in Rotated Sorted Array
Problem: You are given an integer array nums sorted in ascending order that is rotated at some pivot unknown to you beforehand. You should search for a target value in nums. If found return its index; otherwise return -1. You must write an algorithm with O(log n) runtime complexity.
Example:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Read detailed problem statement here.
Challenge 20 — Diameter of Binary Tree
Problem: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is defined as the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Input: root = [1,2,3,4,5]
Output: 3 (the length of path [4 -> 2 -> 1 -> 3] or [5 -> 2 -> 1 -> 3])
Read detailed problem statement here.
Challenge 21 — Smallest Range Covering Elements from K Lists
Problem: You are given k lists, each containing sorted integers in non-decreasing order. Your task is to find the smallest range [a, b] such that at least one number from each of the k lists is included in this range.
Example:
Input: nums = [[1,2,3],[1,2,3],[1,2,3]]
Output: [1,1]
Read detailed problem statement here.
Challenge 22 — Subarray Sum Equals K
Problem: Given an array of integers and an integer k, return the total number of continuous subarrays whose sum equals k.
Example:
Input: nums = [1,1,1], k = 2
Output: 2 (The subarrays are [1,1] and [1,1])
Read detailed problem statement here.
Challenge 23 — Bitwise AND of Numbers Range
Problem: Given a range [m, n], return the bitwise AND of all numbers in this range, inclusive.
Example:
Input: m = 5, n = 7
Output: 4 (The bitwise AND of 5, 6, and 7 is 4)
Read detailed problem statement here.
Challenge 24 — LRU Cache
Problem: Design and implement a data structure for Least Recently Used (LRU) Cache. It should support the following operations: get(key) and put(key, value).
Example:
Input:
LRUCache cache = new LRUCache(2); // capacity
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
Output: 1, -1
Read detailed problem statement here.
Challenge 25 — Jump Game
Problem: You are given an array of non-negative integers where each element represents the maximum jump length at that position. Determine if you can reach the last index.
Example:
Input: nums = [2,3,1,1,4]
Output: true (You can jump to the last index)
Read detailed problem statement here.
Challenge 26 — Longest Common Subsequence
Problem: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence is a sequence that appears in the same relative order but not necessarily contiguous.
Example:
Input: text1 = "abcde", text2 = "ace"
Output: 3 (The longest common subsequence is "ace")
Read detailed problem statement here.
Challenge 27 — Maximal Square
Problem: Given a 2D binary matrix filled with '0's and '1's, find the largest square containing only '1's and return its area.
Example:
Input:
matrix = [
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["0","0","0","0","0"]
]
Output: 4 (The largest square has a side length of 2)
Read detailed problem statement here.
Challenge 28 — First Unique Number or Coin Change
Option 1
Problem: Design a data structure that supports the following operations in average O(1) time complexity: Insert a value into the structure. Return the first unique value. Remove a value from the structure.
Example:
Input:
FirstUnique firstUnique = new FirstUnique([2,3,5]);
firstUnique.showFirstUnique(); // returns 2
firstUnique.add(5); // remove 5 from unique numbers
firstUnique.showFirstUnique(); // returns 3
Output: 2, 3
Read detailed problem statement here.
Option 2
Problem: You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. You want to determine the fewest number of coins that you need to make up that amount. If that amount cannot be made up by any combination of the coins, return -1.
Example:
Input: coins = [1, 2, 5], amount = 11
Output: 3 (11 can be made with one 5 and two 2's)
Read detailed problem statement here.
Challenge 29 — Unique Paths
Problem: A robot is located at the top-left corner of an m x n grid. The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. How many unique paths are there?
Example:
Input: m = 3, n = 7
Output: 28
Read detailed problem statement here.
Challenge 30 — N Queens
Problem: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens threaten each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
Example:
Input: n = 4 Output:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Read detailed problem statement here.
That’s it from the challenge!
If you are still around or you’ve completed all the challenges, kudos to you! Share your achievement with everyone around you. This entire challenge was designed to help you stay consistent with solving LeetCode problems and become a better developer.
If you found value in this LeetCode challenge, don’t forget to share it on Peerlist and tag me! I’d love to hear all your stories.
Let’s keep learning 🚀