Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
# Allow toggling components to install and update based off flags
updateconsul=1
updatedocker=1
updatedockermachine=1
updatedockercompose=1
updatedockerswarm=1
consulversion="0.6.4"
"""
============================================================================
Question : 3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and
Ndisks of different sizes which can slide onto any tower.
The puzzle starts with disks sorted in ascending order of size from top to bottom
(i.e., each disk sits on top of an even larger one).
You have the following constraints:
(1) Only one disk can be moved at a time.
(2) A disk is slid off the top of one tower onto the next tower.
@habina
habina / CC2.7.py
Last active August 29, 2015 14:03
"""
============================================================================
Question : 2.7 Implement a function to check if a linked list is a palindrome.
Solution : Save all elements in to a python list, and use slicing to check if
it is a palindrome
Time Complexity : O(N)
Space Complexity: O(N)
Gist Link : https://gist.github.com/habina/20fd04778919563f0126
============================================================================
"""
"""
============================================================================
Question : 2.6
Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input:A ->B->C->D->E-> C[thesameCasearlier]
Output:C
Solution : Faster pointer moves two steps at a time,
"""
============================================================================
Question : 1.8 Assume you have a method isSubstring which checks if one word is a substring of another.
Given two strings, s1 and s2, write code to check if s2 is
a rotation of s1 using only one call to isSubstring (e.g.,"waterbottle"is a rotation of"erbottlewat").
Solution : Concatenate s1 with s1, check if s2 is a substring of s1 + s1
Time Complexity : Depend on isSubstring()
Space Complexity: O(n)
Gist Link : https://gist.github.com/habina/33b581c69f59fccfc1a6
============================================================================
"""
============================================================================
Question : 2.5
You have two numbers represented by a linked list, where each node contains a single digit.
The digits are stored in reverse order, such that the Ts digit is at the head of the list.
Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE
Input:(7-> 1 -> 6) + (5 -> 9 -> 2).Thatis,617 + 295.
Output: 2 -> 1 -> 9.That is, 912.
FOLLOW UP
"""
============================================================================
Question : 2.4 Write code to partition a linked list around a value x,
such that all nodes less than x come before all nodes greater than or equal to x.
Solution : I have three linkedlist, one for less, one for equal, one for greater, get all value from original linkedlist,
and save it to correspoding linkedlist, lastly connect these three linkedlist.
Time Complexity : O(N)
Space Complexity: O(N)
Gist Link : https://gist.github.com/habina/719d2ca521e7695311c9
============================================================================
"""
============================================================================
Question : Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
EXAMPLE
Input: the node c from the linked list a->b->c->d->e
Result: nothing isreturned, but the new linked list looks like a- >b- >d->e
Solution : Copy the following pointer content, and relink the next to the next next pointer
Time Complexity : O(1)
Space Complexity: O(1)
Gist Link : https://gist.github.com/habina/1effe686ec65dbadf1ec
@habina
habina / CareerCup2.2.py
Last active August 29, 2015 14:03
2.2 Implement an algorithm to find the kth to last element of a singly linked list.
"""
============================================================================
Question : 2.2 Implement an algorithm to find the kth to last element of a singly linked list.
Solution : Faster pointer move forward Kth node first, then move forward to the end alone with slow pointer,
until faster pointer reaches the end, return slower pointer
Time Complexity : O(N)
Space Complexity: O(1)
Gist Link : https://gist.github.com/habina/f521508cef301ac0d18d
============================================================================
"""
"""
============================================================================
Question :Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
Solution : iterate through the whole, add char to a set
if char already existed in set, skip this node
Time Complexity : Average Case: O(1), Worst Case: O(N)
Space Complexity: O(N)
Gist Link : https://gist.github.com/habina/3896531754d629b2032f