This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Not really a "use-case" per se, but I always thought this problem was | |
| # interesting because it has a really simple answer (as you can see below). | |
| # Sometimes it is asked in coding interviews. | |
| # | |
| # How do you implement a Queue using only Stack data structure? | |
| class Node: | |
| def __init__(self, item, next): | |
| self.item = item | |
| self.next = next |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A simple stack, LIFO fo' lyfe | |
| class Node: | |
| def __init__(self, item, next): | |
| self.item = item | |
| self.next = next | |
| class Stack: | |
| top = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Heapsort with Java | |
| // Heap properties: | |
| // Left child of h[i] is h[2i + 1] (given 2i + 1 < h.size) | |
| // Right child of h[i] is h[2i + 2] (given 2i + 2 < h.size) | |
| // parent of h[i] is h[(i-1)/2] (given i > 0) | |
| public class Heap { | |
| int[] heap; | |
| int size; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Reverse a linked list | |
| public class Test { | |
| public static class Node { | |
| public int value; | |
| public Node next; | |
| public Node(int value, Node next) { | |
| this.value = value; | |
| this.next = next; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Test { | |
| // You are given an array with n positive integers where all values in the array are repeated except for one. | |
| // Return the one that is not repeated. | |
| // brute force answer | |
| public static int notRepeated_bf(int[] integers) { | |
| int not_repeated; | |
| int current_num; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. Explain what the big three depth-first tree traversals are and what to consider when implementing them. | |
| The big three depth-first tree traversals are ways to traverse a tree. "Depth-first" means that they will traverse to the lowest depth before moving on to the next node. The three ways are inorder (left, root, right), preorder (root, left, right) and postorder (left, right, root). |