
Sharpen your coding prowess with these thoughtfully curated computer science problems. Much like a “LeetCode” for learners, each problem is aimed at enhancing your algorithmic thinking and programming capabilities. Let’s dive in!
1. Two Sum
Find the pair of numbers in an array that add up to a given sum.
Example:
Array: [2, 7, 11, 15], Target: 9
Solution: [0, 1] (because 2 + 7 = 9)
2. Reverse Integer
Reverse the digits of an integer without converting it into a string.
Example:
Input: 123,
Output: 321
3. Check Anagrams
Determine if two strings are anagrams of each other.
Example:
String 1: “listen”, String 2: “silent”
Output: True
4. First Unique Character
Find the first non-repeating character in a string.
Example:
String: “leetcode”
Output: ‘l’
5. Valid Parentheses
Check if the input string of parentheses is valid (every opening must have a closing).
Example:
String: “()[]{}”
Output: True
6. Merge Sorted Array
Merge two sorted integer arrays into one sorted array.
Example:
Array1: [1,2,3], Array2: [2,5,6]
Output: [1,2,2,3,5,6]
7. Single Number
Find the number that appears only once in an array where all other numbers appear twice.
Example:
Array: [4,1,2,1,2]
Output: 4
8. Move Zeroes
Move all zeroes in an array to the end maintaining the order of non-zero elements.
Example:
Array: [0,1,0,3,12]
Output: [1,3,12,0,0]
9. Buy and Sell Stock
Write an algorithm to find the maximum profit you can achieve from a list of stock prices.
Example:
Prices: [7,1,5,3,6,4]
Output: 5 (Buy on day 2 at price 1 and sell on day 5 at price 6)
10. Contains Duplicate
Check whether any value appears at least twice in the array.
Example:
Array: [1,2,3,1]
Output: True
11. Rotate Image
Rotate an n x n
2D matrix 90 degrees (clockwise).
Example:
Matrix:
[ [1,2,3],
[4,5,6],
[7,8,9] ]
Output:
[ [7,4,1],
[8,5,2],
[9,6,3] ]
12. Valid Sudoku
Determine if a 9×9 Sudoku board is valid.
Example:
Board:
[ [“5”,”3”, “.”, …],
[“6″,”.”, “3”, …],
[“.”, “9”, “8”, …],
… ]
Output: False (since there are two ‘3’s in the second row)
13. Word Search
Given a 2D board and a word, find if the word exists in the grid.
Example:
Board:
[ [‘A’,’B’,’C’,’E’],
[‘S’,’F’,’C’,’S’],
[‘A’,’D’,’E’,’E’] ],
Word: “ABCCED”
Output: True
14. Count Primes
Count the number of prime numbers less than a non-negative number, n.
Example:
Input: 10
Output: 4 (2, 3, 5, 7 are prime)
15. Flood Fill
Perform a “flood fill” on an image represented by a 2D array.
Example:
Image:
[ [1,1,1],
[1,1,0],
[1,0,1] ]
Start: (1, 1), New Color: 2
Output:
[ [2,2,2],
[2,2,0],
[2,0,1] ]
16. Longest Substring Without Repeating Characters
Find the length of the longest substring without repeating characters.
Example:
String: “abcabcbb”
Output: 3 (The answer is “abc”, with the length of 3.)
By progressively working through these problems, akin to the style presented on coding challenge platforms like LeetCode, programmers can enhance their understanding of data structures and algorithms while refining their coding technique. Now, choose a language and start your journey to becoming a more skilled programmer!