HackerRank Solutions in Python3
This is a collection of my HackerRank solutions written in Python3. The goal of this series is to keep the code as concise and efficient as possible. It might not be perfect due to the limitation of my ability and skill, so feel free to make suggestions if you spot something that can be improved.
The index below is auto-generated. See update-challenge-list.py and generate-indices.py.
Index
- Algorithms (60/409)
- Data Structures (11/114)
- Mathematics (1/250)
Algorithms
- Warmup (9/9)
- Implementation (8/59)
- Strings (11/45)
- Sorting (5/18)
- Search (3/23)
- Graph Theory (2/58)
- Greedy (8/22)
- Dynamic Programming (8/94)
- Constructive Algorithms (2/11)
- Bit Manipulation (0/25)
- Recursion (3/10)
- Game Theory (1/31)
- NP Complete (0/4)
Warmup
Implementation
Strings
Sorting
# | Challenge | Solution |
---|---|---|
1 | Intro to Tutorial Challenges | Solution & Comment |
2 | Insertion Sort - Part 1 | Solution & Comment |
3 | Insertion Sort - Part 2 | Solution & Comment |
4 | Correctness and the Loop Invariant | Solution & Comment |
5 | Running Time of Algorithms | WIP |
6 | Quicksort 1 - Partition | WIP |
7 | Quicksort 2 - Sorting | WIP |
8 | Quicksort In-Place | WIP |
9 | Running Time of Quicksort | WIP |
10 | Counting Sort 1 | WIP |
11 | Counting Sort 2 | WIP |
12 | Counting Sort 3 | WIP |
13 | The Full Counting Sort | WIP |
14 | Closest Numbers | WIP |
15 | Find the Median | WIP |
16 | Insertion Sort Advanced Analysis | WIP |
17 | Fraudulent Activity Notifications | WIP |
18 | Lily's Homework | Solution & Comment |
Search
# | Challenge | Solution |
---|---|---|
1 | Hackerland Radio Transmitters | Solution & Comment |
2 | Ice Cream Parlor | Solution & Comment |
3 | Gridland Metro | WIP |
4 | Missing Numbers | WIP |
5 | KnightL on a Chessboard | Solution & Comment |
6 | Pairs | WIP |
7 | Sherlock and Array | WIP |
8 | Maximum Subarray Sum | WIP |
9 | Connected Cells in a Grid | WIP |
10 | Short Palindrome | WIP |
11 | Maximizing Mission Points | WIP |
12 | Count Luck | WIP |
13 | Cut the Tree | WIP |
14 | Making Candies | WIP |
15 | Gena Playing Hanoi | WIP |
16 | Beautiful Quadruples | WIP |
17 | Bike Racers | WIP |
18 | Task Scheduling | WIP |
19 | Similar Pair | WIP |
20 | Absolute Element Sums | WIP |
21 | Sorted Subsegments | WIP |
22 | Distant Pairs | WIP |
23 | King Richard's Knights | WIP |
Graph Theory
Greedy
# | Challenge | Solution |
---|---|---|
1 | Minimum Absolute Difference in an Array | Solution & Comment |
2 | Marc's Cakewalk | WIP |
3 | Grid Challenge | WIP |
4 | Luck Balance | Solution & Comment |
5 | Maximum Perimeter Triangle | WIP |
6 | Beautiful Pairs | Solution & Comment |
7 | Sherlock and The Beast | Solution & Comment |
8 | Priyanka and Toys | WIP |
9 | Largest Permutation | Solution & Comment |
10 | Mark and Toys | Solution & Comment |
11 | Greedy Florist | WIP |
12 | Max Min | WIP |
13 | Jim and the Orders | WIP |
14 | Permuting Two Arrays | WIP |
15 | Cutting Boards | WIP |
16 | Reverse Shuffle Merge | Solution & Comment |
17 | Goodland Electricity | WIP |
18 | Chief Hopper | Solution & Comment |
19 | Sherlock and MiniMax | WIP |
20 | Accessory Collection | WIP |
21 | Team Formation | WIP |
22 | Fighting Pits | WIP |
Dynamic Programming
Constructive Algorithms
# | Challenge | Solution |
---|---|---|
1 | Lena Sort | WIP |
2 | Flipping the Matrix | WIP |
3 | Gaming Array | Solution & Comment |
4 | New Year Chaos | WIP |
5 | Bonetrousle | Solution & Comment |
6 | Yet Another KMP Problem | WIP |
7 | Beautiful 3 Set | WIP |
8 | Inverse RMQ | WIP |
9 | Two Subarrays | WIP |
10 | Lovely Triplets | WIP |
11 | Array Construction | WIP |
Bit Manipulation
# | Challenge | Solution |
---|---|---|
1 | Lonely Integer | WIP |
2 | Maximizing XOR | WIP |
3 | Counter game | WIP |
4 | Xor-sequence | WIP |
5 | Sum vs XOR | WIP |
6 | Flipping bits | WIP |
7 | Yet Another Minimax Problem | WIP |
8 | Sansa and XOR | WIP |
9 | AND Product | WIP |
10 | Xoring Ninja | WIP |
11 | Cipher | WIP |
12 | XOR Matrix | WIP |
13 | What's Next? | WIP |
14 | String Transmission | WIP |
15 | A or B | WIP |
16 | Manipulative Numbers | WIP |
17 | Stone game | WIP |
18 | 2's complement | WIP |
19 | Changing Bits | WIP |
20 | XOR key | WIP |
21 | Maximizing the Function | WIP |
22 | XOR Subsequences | WIP |
23 | Iterate It | WIP |
24 | Hamming Distance | WIP |
25 | Mixing proteins | WIP |
Recursion
# | Challenge | Solution |
---|---|---|
1 | The Power Sum | Solution & Comment |
2 | Crossword Puzzle | Solution & Comment |
3 | Recursive Digit Sum | WIP |
4 | Simplified Chess Engine | WIP |
5 | Password Cracker | WIP |
6 | Arithmetic Expressions | Solution & Comment |
7 | K Factorization | WIP |
8 | Bowling Pins | WIP |
9 | Simplified Chess Engine II | WIP |
10 | Repetitive K-Sums | WIP |
Game Theory
# | Challenge | Solution |
---|---|---|
1 | Game of Stones | Solution & Comment |
2 | Tower Breakers | WIP |
3 | A Chessboard Game | WIP |
4 | Introduction to Nim Game | WIP |
5 | Mis¨¨re Nim | WIP |
6 | Nimble Game | WIP |
7 | Alice and Bob's Silly Game | WIP |
8 | Poker Nim | WIP |
9 | Tower Breakers, Revisited! | WIP |
10 | Tower Breakers, Again! | WIP |
11 | Zero-Move Nim | WIP |
12 | Chessboard Game, Again! | WIP |
13 | Digits Square Board | WIP |
14 | Fun Game | WIP |
15 | Stone Division | WIP |
16 | Chocolate in Box | WIP |
17 | Kitty and Katty | WIP |
18 | Powers Game | WIP |
19 | Deforestation | WIP |
20 | Bob and Ben | WIP |
21 | Tower Breakers - The Final Battle | WIP |
22 | Simple Game | WIP |
23 | Permutation game | WIP |
24 | Move the Coins | WIP |
25 | Play on benders | WIP |
26 | New Year Game | WIP |
27 | Stone Piles | WIP |
28 | Chocolate Game | WIP |
29 | Manasa and Prime game | WIP |
30 | Vertical Rooks | WIP |
31 | A stones game | WIP |
NP Complete
# | Challenge | Solution |
---|---|---|
1 | Walking the Approximate Longest Path | WIP |
2 | Sam's Puzzle (Approximate) | WIP |
3 | Spies, Revised | WIP |
4 | TBS Problem | WIP |
Data Structures
- Arrays (6/6)
- Linked Lists (0/15)
- Trees (0/15)
- Balanced Trees (0/3)
- Stacks (0/9)
- Queues (0/5)
- Heap (0/4)
- Disjoint Set (4/4)
- Multiple Choice (0/3)
- Trie (0/2)
- Advanced (1/48)
Arrays
Linked Lists
# | Challenge | Solution |
---|---|---|
1 | Print the Elements of a Linked List | WIP |
2 | Insert a Node at the Tail of a Linked List | WIP |
3 | Insert a node at the head of a linked list | WIP |
4 | Insert a node at a specific position in a linked list | WIP |
5 | Delete a Node | WIP |
6 | Print in Reverse | WIP |
7 | Reverse a linked list | WIP |
8 | Compare two linked lists | WIP |
9 | Merge two sorted linked lists | WIP |
10 | Get Node Value | WIP |
11 | Delete duplicate-value nodes from a sorted linked list | WIP |
12 | Cycle Detection | WIP |
13 | Find Merge Point of Two Lists | WIP |
14 | Inserting a Node Into a Sorted Doubly Linked List | WIP |
15 | Reverse a doubly linked list | WIP |
Trees
# | Challenge | Solution |
---|---|---|
1 | Tree: Preorder Traversal | WIP |
2 | Tree: Postorder Traversal | WIP |
3 | Tree: Inorder Traversal | WIP |
4 | Tree: Height of a Binary Tree | WIP |
5 | Tree : Top View | WIP |
6 | Tree: Level Order Traversal | WIP |
7 | Binary Search Tree : Insertion | WIP |
8 | Tree: Huffman Decoding | WIP |
9 | Binary Search Tree : Lowest Common Ancestor | WIP |
10 | Swap Nodes [Algo] | WIP |
11 | Kitty's Calculations on a Tree | WIP |
12 | Is This a Binary Search Tree? | WIP |
13 | Square-Ten Tree | WIP |
14 | Balanced Forest | WIP |
15 | Tree Coordinates | WIP |
Balanced Trees
# | Challenge | Solution |
---|---|---|
1 | Self Balancing Tree | WIP |
2 | Array and simple queries | WIP |
3 | Median Updates | WIP |
Stacks
# | Challenge | Solution |
---|---|---|
1 | Maximum Element | WIP |
2 | Balanced Brackets | WIP |
3 | Equal Stacks | WIP |
4 | Game of Two Stacks | WIP |
5 | Largest Rectangle | WIP |
6 | Simple Text Editor | WIP |
7 | Poisonous Plants | WIP |
8 | AND xor OR | WIP |
9 | Waiter | WIP |
Queues
# | Challenge | Solution |
---|---|---|
1 | Queue using Two Stacks | WIP |
2 | Castle on the Grid | WIP |
3 | Down to Zero II | WIP |
4 | Truck Tour | WIP |
5 | Queries with Fixed Length | WIP |
Heap
# | Challenge | Solution |
---|---|---|
1 | QHEAP1 | WIP |
2 | Jesse and Cookies | WIP |
3 | Find the Running Median | WIP |
4 | Minimum Average Waiting Time | WIP |
Disjoint Set
# | Challenge | Solution |
---|---|---|
1 | Merging Communities | Solution & Comment |
2 | Components in a graph | Solution & Comment |
3 | Kundu and Tree | Solution & Comment |
4 | Super Maximum Cost Queries | Solution & Comment |
Multiple Choice
# | Challenge | Solution |
---|---|---|
1 | Data Structures MCQ 1 | WIP |
2 | Data Structures MCQ 2 | WIP |
3 | Data Structures MCQ 3 | WIP |
Trie
# | Challenge | Solution |
---|---|---|
1 | Contacts | WIP |
2 | No Prefix Set | WIP |
Advanced
Mathematics
- Fundamentals (0/28)
- Number Theory (0/67)
- Combinatorics (0/53)
- Algebra (0/35)
- Geometry (1/26)
- Probability (0/24)
- Linear Algebra Foundations (0/17)
Fundamentals
# | Challenge | Solution |
---|---|---|
1 | Find the Point | WIP |
2 | Maximum Draws | WIP |
3 | Handshake | WIP |
4 | Leonardo's Prime Factors | WIP |
5 | Connecting Towns | WIP |
6 | Summing the N series | WIP |
7 | Sherlock and Moving Tiles | WIP |
8 | Restaurant | WIP |
9 | Reverse Game | WIP |
10 | Strange Grid Again | WIP |
11 | Diwali Lights | WIP |
12 | Sherlock and Divisors | WIP |
13 | Sherlock and Permutations | WIP |
14 | Even Odd Query | WIP |
15 | Special Multiple | WIP |
16 | Matrix Tracing | WIP |
17 | Die Hard 3 | WIP |
18 | Halloween party | WIP |
19 | Filling Jars | WIP |
20 | Is Fibo | WIP |
21 | K Candy Store | WIP |
22 | Sumar and the Floating Rocks | WIP |
23 | Russian Peasant Exponentiation | WIP |
24 | Bus Station | WIP |
25 | Most Distant | WIP |
26 | Jim and the Jokes | WIP |
27 | Possible Path | WIP |
28 | Mutual Recurrences | WIP |
Number Theory
Combinatorics
Algebra
# | Challenge | Solution |
---|---|---|
1 | Stepping Stones Game | WIP |
2 | Shashank and List | WIP |
3 | Triangle Numbers | WIP |
4 | Little Gaurav and Sequence | WIP |
5 | Easy sum | WIP |
6 | Difference and Product | WIP |
7 | Pairwise Sum and Divide | WIP |
8 | Tell the Average | WIP |
9 | Game Of Rotation | WIP |
10 | Hackonacci Matrix Rotations | WIP |
11 | Power Calculation | WIP |
12 | Sherlock and Square | WIP |
13 | Wet Shark and 42 | WIP |
14 | Sherlock and Queries | WIP |
15 | Manasa and Sub-sequences | WIP |
16 | Summing the K-N series | WIP |
17 | XOR love | WIP |
18 | Manasa and Calculations | WIP |
19 | Simple One | WIP |
20 | Circle Summation | WIP |
21 | Girlfriend & Necklace | WIP |
22 | Manasa and Pizza | WIP |
23 | Signal Tower | WIP |
24 | Summing the K-N-R Series | WIP |
25 | Emma and sum of products | WIP |
26 | Fun With Series | WIP |
27 | Permutation Equations | WIP |
28 | Counting Equations | WIP |
29 | Calculate It | WIP |
30 | Introduction to Algebra | WIP |
31 | Cross Matrix | WIP |
32 | Introduction to Algebra 2 | WIP |
33 | The Black Box | WIP |
34 | Black Hole | WIP |
35 | Minimal Cyclic Shift | WIP |
Geometry
# | Challenge | Solution |
---|---|---|
1 | Points On a Line | WIP |
2 | Rectangular Game | Solution & Comment |
3 | Sherlock and Counting | WIP |
4 | Sherlock and Planes | WIP |
5 | Circle City | WIP |
6 | Xrange's Pancakes | WIP |
7 | Spheres | WIP |
8 | Baby Step, Giant Step | WIP |
9 | Jim Beam | WIP |
10 | Hyperspace Travel | WIP |
11 | Solve Equations | WIP |
12 | Stars | WIP |
13 | Polar Angles | WIP |
14 | Sherlock and Geometry | WIP |
15 | Good Point | WIP |
16 | Polygons | WIP |
17 | Polygon | WIP |
18 | Meeting Point | WIP |
19 | House Location | WIP |
20 | Jim and the Challenge | WIP |
21 | The Letter N | WIP |
22 | Best Sum | WIP |
23 | Geometry Queries | WIP |
24 | Count Triangles | WIP |
25 | Isosceles Triangles | WIP |
26 | Elastic rope | WIP |
Probability
# | Challenge | Solution |
---|---|---|
1 | Random number generator | WIP |
2 | Sherlock and Probability | WIP |
3 | Day 4: Normal Distribution #1 | WIP |
4 | Day 4: Normal Distribution #2 | WIP |
5 | B'day Gift | WIP |
6 | Normal Distribution #3 | WIP |
7 | Palindromes | WIP |
8 | Lazy Sorting | WIP |
9 | Binomial Distribution #1 | WIP |
10 | Kevin and Expected Value | WIP |
11 | Binomial Distribution #2 | WIP |
12 | The White Lotus and Caterpillar game | WIP |
13 | Binomial Distribution #3 | WIP |
14 | Assignment Problem | WIP |
15 | Connect the country | WIP |
16 | Random Integers Random Bits | WIP |
17 | Dice Stats | WIP |
18 | Random Number Generator | WIP |
19 | Zombie March*** | WIP |
20 | Bear and Dancing | WIP |
21 | Randomness | WIP |
22 | Random | WIP |
23 | Mathematical Expectation | WIP |
24 | Colorful Polygon | WIP |