Skip to content

Instantly share code, notes, and snippets.

View thobbiz's full-sized avatar
💭
passion codes, I debug

Thobbiz thobbiz

💭
passion codes, I debug
  • Thobbiz
  • Lagos
  • 00:38 (UTC +01:00)
  • X @thobbizz
View GitHub Profile
@thobbiz
thobbiz / main.md
Created February 11, 2026 22:19
Maximum Depth of Binary Tree

Question

Approach

my approach was to use bottom-up --finding the depth of the subtrees for each node--. I used recursion for the approach

Algorithm

  • if root is None:
    • return 0
  • leftHeight = self.maxDepth(root.left)
  • rightHeight = self.maxDepth(root.right)
@thobbiz
thobbiz / main.md
Created February 10, 2026 21:46
LRU Cache

Question

Approach

my original approach was to use a dictionary(obviously) and then to use a double-ended queue(deque) for the LRU operation. It worked but it was only about 5% efficient and I couldn't accept that. So I went web surfing for another faster method, then I saw the Doubly-Linked list method which was wayy easier and faster because the "moving to the front" and "removing LRU" part in deques involves shifting(slower) which is O(n) time. While the same task in the doublly linked list takes O(1) time

Algorithm

Initialize:

@thobbiz
thobbiz / main.md
Created February 9, 2026 22:04
Copy List with Random Pointer

Question

Approach

my approach is to use a create a copy of the list without the random value yet..while making it; i will use a dictionary to map each address to a node(for o(1) access). Since all the nodes have been created, then I will map their random pointers using the dictionary to get each node.

Algorithm

Initialize:

  • hashMap: Empty dictionary, serves a dictionary for O(1) access time
@thobbiz
thobbiz / main.md
Created February 8, 2026 22:40
Time Based Key-Value Store

Question

Approach

my approach is to use a dictionary that connects a key(str) to a list(for value and timestamp)

Algorithm

  • Check Existence: If the key does not exist in your map, return an empty string immediately.
  • Access the Timeline: Retrieve the list of [timestamp, value] pairs associated with that key.
  • Initialize Pointers:
  • Set low = 0
@thobbiz
thobbiz / main.md
Created February 7, 2026 22:55
Invert Binary Tree

Question

Approach

my approach is simple, have a stack, pop from the top, go through the current node swap the children, check if the children exists; if it does, add it to a stack

Algorithm

  • Base Case:
    • If the current node is None (or nil), return None. This stops the recursion at the leaves.
  • The Swap:
@thobbiz
thobbiz / main.md
Created February 6, 2026 21:48
Find the Duplicate Number

Question

Approach

question is very simple, i will go thorugh the numbers, add the unknown ones in a map and if it is in the map return it

Algorithm

  • Initialize:
    • variable history of type set(for n lookup)
  • integer n of length of nums - 1.
@thobbiz
thobbiz / main.md
Created February 5, 2026 22:31
Remove Nth Node from End of List

Question

Approach

I used an approach using 2 pointers such that one moves n + 1 distance from the other. They then both travel one node each until the faster one gets to the end. By this method the slow pointer is directly behind the fast one such that we can just remove it from the linked list by putting the next to the one after the node to-be-deleted. I used a dummy node to point to the head for simplification

Algorithm

  • Initialize a Dummy Node: Create a sentinel dummy node and point its next attribute to the head. This handles edge cases, such as removing the first node of the list.
@thobbiz
thobbiz / main.md
Created February 4, 2026 22:41
Reorder List

Question

Approach

To reorder the linked list as required, we need to rearrange the nodes by taking one from the front and then one from the back, continuing this alternation until the list is fully reordered.We split the list in half, reverse the second half, and then merge both halves by alternating nodes. This ensures we maintain the original relative ordering while meeting the required pattern.

Algorithm

  • Use two pointers (slow and fast) to find the middle of the linked list.
  • Split the list into two halves at the midpoint.
  • Reverse the second half of the list in-place.
@thobbiz
thobbiz / main.md
Created February 3, 2026 22:25
Linked List Cycle

Question

Approach

My approach is to move through the linked list and check if it is in a set; if it is there return true. Else add it to the map.

Algorithm

  • history: An empty set to store the memory addresses (references) of the nodes we have already visited.
  • Current Pointer: Use the head pointer to traverse the list.

Iterate: While head is not None:

@thobbiz
thobbiz / main.md
Created February 2, 2026 21:53
Merge Two Sorted Lists

Question

Approach

I am proud to say i solved this myself.

Algorithm

Initialize:

  • dummy: Create a new placeholder node (often called a "sentinel") to serve as the fixed starting point of the merged list. This avoids handling the head as a special case.
  • cur: Set to the dummy node; this pointer will move forward as you "stitch" the nodes together in sorted order.

Execution (Loop while both list1 and list2 are not None):