LeetCode Practice Questions
This article provides basic and foundational logic thinking to improve problem solving mindset through solving most frequest asked LeetCode interview questions.
LeetCode Practice Questions
๐งฎ Math & Number Theory Problems
- Sum of N Even Natural Numbers
- Check for Even Number
- Check for Prime Number
- Valid Perfect Square
- Decimal to Binary
- Binary to Decimal
- GCD of Two Numbers
๐ข List & Sequence Problems
- Sum of List Elements
- Largest Element in a List
- Remove Duplicate in a List
- Check if All Elements in a List are Unique
- Program to Reverse a List
- Count Number of Odd and Even Elements in a List
- Maximum Difference Between Two Consecutive Elements in a List
- Merge Two Sorted Lists
- Rotate a List
- Merge 2 Lists into Dictionary
- Merge Multiple Dictionaries
- Words Frequency in a Sentence
- Palindromic Tuple
- Merge Dictionaries with Common Keys
- Check if List is Subset of Another List
๐ String Problems
- Reverse a String
- Count Vowels in a String
- Check for Same Strings
- Check Palindrome
- Count Words in a String
- Remove Duplicates in a String
- Count Consonants in a String
- Check for Anagrams
- Check Subsequence
- Check for Substring
- Length of the Longest Word
๐ท Pattern Printing Problems
- Square of Side ‘N’
- Hollow Square of Side ‘N’
- Rectangle Pattern
- Right Angled Triangle
- Inverted Right Angled Triangle
- Pyramid Pattern
- Inverted Pyramid Pattern
- Right Angled Triangle with Numbers
- Floydโs Triangle
- Diamond Pattern
- Right Angled Triangle II
- Sandglass Pattern
- Hollow Right Triangle
- Hollow Inverted Right Triangle
- Number Pyramid Pattern
๐งฐ Miscellaneous Basic Programs
- Celsius to Fahrenheit
- Area of a Rectangle
- Distance Covered by a Vehicle
- Number of Rounds of Lift
- Line Equation
Pattern Practice Questions
1. Square of side ‘N’
Input:N = 4
Output:
****
****
****
****
Constraints: 1 <= N <= 100
2. Hollow Square of side ‘N’
Input:N = 5
Output:
*****
* *
* *
* *
*****
Constraints: 2 <= N <= 100
3. Rectangle Pattern
Input:R = 3, C = 6
Output:
******
******
******
Constraints: 1 <= R, C <= 100
4. Right Angled Triangle
Input:N = 5
Output:
*
**
***
****
*****
Constraints: 1 <= N <= 100
5. Inverted Right Angled Triangle
Input:N = 5
Output:
*****
****
***
**
*
Constraints: 1 <= N <= 100
6. Pyramid Pattern
Input:N = 4
Output:
*
***
*****
*******
Constraints: 1 <= N <= 100
7. Inverted Pyramid Pattern
Input:N = 4
Output:
*******
*****
***
*
Constraints: 1 <= N <= 100
8. Right Angled Triangle with Numbers
Input:N = 5
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Constraints: 1 <= N <= 100
9. Floydโs Triangle
Input:N = 5
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Constraints: 1 <= N <= 100
10. Diamond Pattern
Input:N = 3
Output:
*
***
*****
***
*
Constraints: 1 <= N <= 50
11. Right Angled Triangle II (Alphabets)
Input:N = 5
Output:
A
A B
A B C
A B C D
A B C D E
Constraints: 1 <= N <= 26
12. Sandglass Pattern
Input:N = 4
Output:
********
******
****
**
**
****
******
********
Constraints: 1 <= N <= 50
13. Hollow Right Triangle
Input:N = 5
Output:
*
**
* *
* *
*****
Constraints: 2 <= N <= 100
14. Hollow Inverted Right Triangle
Input:N = 5
Output:
*****
* *
* *
**
*
Constraints: 2 <= N <= 100
15. Number Pyramid Pattern
Input:N = 4
Output:
1
1 2
1 2 3
1 2 3 4
Constraints: 1 <= N <= 100
Functions practice questions
1. ๐ฅ Celsius to Fahrenheit Converter
Description:
Convert temperature from Celsius to Fahrenheit using the formula:
F = (C ร 9/5) + 32
Input:int C
โ Temperature in Celsius
Output:float
โ Temperature in Fahrenheit (rounded to 2 decimal places)
Constraints:
-100 <= C <= 100
Sample Input:
25
Expected Output:
77.00
2. ๐ Area of a Rectangle
Description:
Calculate the area of a rectangle using the formula:
Area = Length ร Breadth
Input:int L, B
โ Length and Breadth of the rectangle
Output:int
โ Area of the rectangle
Constraints:
1 <= L, B <= 10^4
Sample Input:
15 10
Expected Output:
150
3. ๐ Distance Covered by a Vehicle
Description:
Calculate the distance covered using the formula:
Distance = Speed ร Time
Input:int speed, int time
Output:int
โ Total distance in km
Constraints:
1 <= speed <= 300
1 <= time <= 24
Sample Input:
60 5
Expected Output:
300
4. ๐๏ธ Number of Rounds of Lift
Description:
Given the total number of people P
and lift capacity C
, calculate the minimum number of rounds the lift needs to make to carry all people.
Input:int P, int C
Output:int
โ Minimum number of rounds required
Constraints:
1 <= P <= 10^5
1 <= C <= 100
Sample Input:
27 8
Expected Output:
4
Explanation:
8 + 8 + 8 + 3 โ 4 rounds
5. ๐ Line Equation (y = mx + c)
Description:
Given values of m
, x
, and c
, compute the value of y
using:
y = m * x + c
Input:int m, x, c
Output:int
โ Computed value of y
Constraints:
-10^3 <= m, x, c <= 10^3
Sample Input:
2 4 3
Expected Output:
11
In-built Data Structure Practice Questions
- Title
- Problem Statement
- Input Format
- Output Format
- Constraints
- Sample Input/Output
- Expected Output
1. โ Sum of List Elements
Description:
Calculate the sum of all elements in a list.
Input:List[int] nums
Output:int
โ Sum of elements
Constraints:
1 <= len(nums) <= 10^4
-10^6 <= nums[i] <= 10^6
Sample Input:[1, 2, 3, 4, 5]
Expected Output:15
2. โ Largest Element in a List
Description:
Return the largest number in the list.
Input:List[int] nums
Output:int
โ Maximum element
Constraints:
1 <= len(nums) <= 10^4
Sample Input:[4, 17, 9, 1]
Expected Output:17
3. โ Remove Duplicates in a List
Description:
Remove all duplicate values from a list while preserving order.
Input:List[int] nums
Output:List[int]
โ List without duplicates
Constraints:
1 <= len(nums) <= 10^4
Sample Input:[1, 2, 2, 3, 1]
Expected Output:[1, 2, 3]
4. โ Check if All Elements in a List are Unique
Description:
Check if a list contains all unique values.
Input:List[int] nums
Output:bool
โ True
if all elements are unique, else False
Constraints:
1 <= len(nums) <= 10^5
Sample Input:[1, 2, 3, 4]
Expected Output:True
5. โ Program to Reverse a List
Description:
Return the reversed list.
Input:List[Any] items
Output:List[Any]
โ Reversed list
Sample Input:[1, 2, 3, 4]
Expected Output:[4, 3, 2, 1]
6. โ Count Number of Odd and Even Elements in a List
Description:
Count and return how many elements are odd and how many are even.
Input:List[int] nums
Output:Tuple[int, int]
โ (odd_count, even_count)
Sample Input:[1, 2, 3, 4, 5]
Expected Output:(3, 2)
7. โ Maximum Difference Between Two Consecutive Elements
Description:
Find the max absolute difference between any two consecutive elements.
Input:List[int] nums
Output:int
Sample Input:[2, 9, 4, 1, 7]
Expected Output:7
8. โ Merge Two Sorted Lists
Description:
Merge two sorted lists into a single sorted list.
Input:List[int] list1, List[int] list2
Output:List[int]
โ Sorted merged list
Sample Input:[1, 3, 5], [2, 4, 6]
Expected Output:[1, 2, 3, 4, 5, 6]
9. โ Rotate a List
Description:
Rotate the list k
times to the right.
Input:List[int] nums, int k
Output:List[int]
Sample Input:[1, 2, 3, 4, 5], k = 2
Expected Output:[4, 5, 1, 2, 3]
10. โ Merge 2 Lists into Dictionary
Description:
Given two lists of equal length, create a dictionary with keys from first and values from second.
Input:List[str] keys, List[Any] values
Output:Dict[str, Any]
Sample Input:['a', 'b', 'c'], [1, 2, 3]
Expected Output:{'a': 1, 'b': 2, 'c': 3}
11. โ Merge Multiple Dictionaries
Description:
Merge all provided dictionaries into one. Later keys overwrite earlier ones.
Input:List[Dict] dicts
Output:Dict
โ Merged dictionary
Sample Input:[{'a': 1}, {'b': 2}, {'a': 3}]
Expected Output:{'a': 3, 'b': 2}
12. โ Word Frequency in a Sentence
Description:
Count frequency of each word in a given string.
Input:str sentence
Output:Dict[str, int]
Sample Input:"this is a test this is"
Expected Output:{'this': 2, 'is': 2, 'a': 1, 'test': 1}
13. โ Palindromic Tuple
Description:
Given a tuple of strings, return only palindromic elements.
Input:Tuple[str] items
Output:List[str]
โ Palindromes only
Sample Input:('madam', 'racecar', 'hello')
Expected Output:['madam', 'racecar']
14. โ Merge Dictionaries with Common Keys
Description:
Merge dictionaries by summing values of common keys.
Input:List[Dict[str, int]] dicts
Output:Dict[str, int]
Sample Input:[{'a': 1, 'b': 2}, {'a': 3, 'c': 4}]
Expected Output:{'a': 4, 'b': 2, 'c': 4}
15. โ Check if List is Subset of Another List
Description:
Return True
if all elements of list A exist in list B.
Input:List[int] A, List[int] B
Output:bool
Sample Input:[1, 2], [1, 2, 3, 4]
Expected Output:True
Mathematical Practice Questions
1. โ Sum of N Even Natural Numbers
Description:
Calculate the sum of the first N
even natural numbers.
Input:int N
Output:int
โ Sum of the first N
even numbers
Constraints:
1 <= N <= 10^5
Sample Input:5
Expected Output:30
Explanation: Even numbers = 2 + 4 + 6 + 8 + 10 = 30
2. โ Check for Even Number
Description:
Check if a number is even.
Input:int num
Output:bool
โ True
if even, else False
Constraints:
-10^9 <= num <= 10^9
Sample Input:7
Expected Output:False
3. โ Check for Prime Number
Description:
Check whether a number is prime.
Input:int num
Output:bool
โ True
if prime, else False
Constraints:
0 <= num <= 10^6
Sample Input:13
Expected Output:True
4. โ Valid Perfect Square
Description:
Determine whether a number is a perfect square.
Input:int num
Output:bool
โ True
if perfect square, else False
Constraints:
0 <= num <= 10^10
Sample Input:36
Expected Output:True
5. โ Decimal to Binary
Description:
Convert a non-negative integer to binary (without using built-in bin()
).
Input:int num
Output:str
โ Binary representation
Constraints:
0 <= num <= 10^9
Sample Input:10
Expected Output:1010
6. โ Binary to Decimal
Description:
Convert a binary string to its decimal equivalent.
Input:str binary
Output:int
โ Decimal representation
Constraints:
1 <= len(binary) <= 32
Binary contains only
'0'
or'1'
Sample Input:"1010"
Expected Output:10
7. โ GCD of Two Numbers
Description:
Find the Greatest Common Divisor (GCD) of two numbers.
Input:int a, int b
Output:int
โ GCD of a
and b
Constraints:
1 <= a, b <= 10^9
Sample Input:20 28
Expected Output:4
String Practice Questions
1. ๐ Reverse a String
Description:
Reverse the characters of the given string.
Input:str s
Output:str
โ Reversed string
Constraints:
1 <= len(s) <= 10^5
Sample Input:"hello"
Expected Output:"olleh"
2. ๐ก Count Vowels in a String
Description:
Count the number of vowels (a, e, i, o, u
) in the string.
Input:str s
Output:int
โ Number of vowels
Constraints:
1 <= len(s) <= 10^5
Sample Input:"education"
Expected Output:5
3. ๐ Check for Same Strings
Description:
Return True
if two strings are exactly the same, case-sensitive.
Input:str s1, str s2
Output:bool
Constraints:
1 <= len(s1), len(s2) <= 10^5
Sample Input:"OpenAI", "OpenAI"
Expected Output:True
4. ๐ Check Palindrome
Description:
Check whether the given string is a palindrome (reads the same backward).
Input:str s
Output:bool
Constraints:
1 <= len(s) <= 10^5
Sample Input:"racecar"
Expected Output:True
5. ๐ Count Words in a String
Description:
Return the number of words in a sentence (words separated by whitespace).
Input:str sentence
Output:int
Constraints:
1 <= len(sentence) <= 10^5
Sample Input:"This is a test"
Expected Output:4
6. ๐ Remove Duplicates in a String
Description:
Return the string with all duplicate characters removed, preserving the first occurrence.
Input:str s
Output:str
Constraints:
1 <= len(s) <= 10^5
Sample Input:"banana"
Expected Output:"ban"
7. ๐งฎ Count Consonants in a String
Description:
Return the number of consonants in the given string. Ignore spaces and vowels.
Input:str s
Output:int
Constraints:
1 <= len(s) <= 10^5
Sample Input:"hello world"
Expected Output:7
8. ๐ Check for Anagrams
Description:
Check if two strings are anagrams (same characters, different order).
Input:str s1, str s2
Output:bool
Constraints:
1 <= len(s1), len(s2) <= 10^5
Sample Input:"listen", "silent"
Expected Output:True
9. โ Check Subsequence
Description:
Check if s1
is a subsequence of s2
.
Input:str s1, str s2
Output:bool
Constraints:
1 <= len(s1), len(s2) <= 10^5
Sample Input:"abc", "aebdc"
Expected Output:True
10. ๐ Check for Substring
Description:
Check if one string is a substring of another.
Input:str small, str large
Output:bool
Constraints:
1 <= len(small), len(large) <= 10^5
Sample Input:"test", "this is a test string"
Expected Output:True
11. ๐ Length of the Longest Word
Description:
Return the length of the longest word in a sentence.
Input:str sentence
Output:int
Constraints:
1 <= len(sentence) <= 10^5
Sample Input:"The quick brown fox jumps"
Expected Output:5
(“quick” or “jumps”)