Tower of Hanoi

Problem In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one). You have the following constraints: (A) Only one disk can be moved at a time. (B) A disk is slid off the top of one rod onto the next rod. (C) A disk can only be placed on top of a larger disk. ...

Generate Parentheses Problem

Problem Implement an algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses. OR Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Follow up Make sure the returned list of strings are sorted. Examples Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] ...

Inorder Successor in Binary Search Tree Using Parent link

Problem Given a node in a binary search tree, return the next bigger element, also known as the inorder successor. You can assume each node has a parent pointer. Definition See the definition here. Here is gist though: The in-order successor of a node in a BST is the next node in in-order traversal, which, for a given node, is the node with the smallest value larger than the given node’s value. ...

Unique Paths in Grid 1 - Count all paths moving right or down

Problem There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. ...

What happens when you type an URL in the browser and press enter

The diagram below illustrates the steps. --- title: Refreshing an Expired Access Token --- sequenceDiagram autonumber actor User participant Browser participant DNSCache participant DNSResolver participant Server User->>Browser: enters an URL in the browser say https://k5kc.com/cs/algorithms/2sum-problem Browser->>DNSCache: Look up IP Browser->>DNSResolver: Send DNS Query (If not found in dns cache) DNSResolver-->>Browser: Resolve URL to IP Address Browser->>Server: Establishes TCP connection Browser->>Server: Send HTTP/HTTPS Request Server-->>Browser: Process Request and Send Response Browser->>Browser: Render HTML Content Browser->>Browser: Fetch and Execute CSS/JavaScript Browser-->>User: Page Fully Loads User-->>Browser: Possible Interactions ...

Convert Sorted Array to height-balanced Binary Search Tree

Problem Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. Examples Example 1: Input: nums = [1, 2, 3, 4, 5, 6] Output: [3,2,5,1,null,4,6] Explanation: [4,2,5,1,3,null,6] is also accepted. ...

N-Queens Problem

Problem The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. Each solution contains a distinct board configuration of the n-queens’ placement, where 'Q' and '.' both indicate a queen and an empty space, respectively. ...

Print binary representation of a decimal number

Problem Given a (decimal – e.g. 3.72) number that is passed in as a string, print the binary representation. If the number can not be represented accurately in binary, print “ERROR”. Examples Example 1: Input: num = 3.625 Output: 11.101 Example 2: Input: num = 3.26 Output: ERROR Explanation: There are accuracy issues with floating point numbers. ...

April 14, 2023 · 4 min · TagsList of tags for the post  ctci ·  bits

Create linked lists of all the nodes at each depth for a Binary Tree

Problem Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists) Examples Example 1: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: (1) => NULL (2) => (3) => NULL (4) => (5) => (6) => (7) => NULL ...

Find duplicate elements in the array with 4KB memory only

Problem You have an array with all the numbers from 1 to N, where N is at most 32,000. The array may have duplicate entries and you do not know what N is. With only 4KB of memory available, how would you print all duplicate elements in the array? Solution 4KB = 32768 bits. We can use a byte array to represent if we have seen number i. If so, we make it 1. If we encounter a duplicate, we would have marked the particular position with 1, so we would know we have a duplicate. We have 4KB of memory which means we can address up to 8 * 4 * (2^10) bits. Note that 32*(2^10) bits is greater than 32000. We can create a bit vector with 32000 bits, where each bit represents one integer. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy