Skip to content

Instantly share code, notes, and snippets.

@humbroll
humbroll / linked_list_sum_lists_2_5.py
Created August 28, 2016 04:34
linked list sum lists
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Question.
# You have two numbers represented by a linked list, where each node contains a single
# digit. The digigts are stored in reverse order, such that the 1's 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). That is, 617 + 295
# Output: 2 -> 1 -> 9. That is, 912.
@humbroll
humbroll / partition_linked_list_2_4.py
Created August 28, 2016 04:34
partition linked list
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Question.
# Write code to partition a linked list around a value x, such that nodes less than x come before
# all nodes greater than or equal to x. If x is contained within the list, the values of x only need
# to be after the elements less than x(see below). The partition element x can appear anywhere in the
# "right partition"; it does not need to apeear between the left and right partitions.
# Example
# Input : 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1[partition=5]
@humbroll
humbroll / delete_middle_node_2_3.py
Created August 14, 2016 06:04
Delete middle node in LinkedList
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 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?
class Node:
def __init__(self, value, next=None):
@humbroll
humbroll / remove_dups_2_1.py
Created August 14, 2016 06:03
Remove Duplication in LinkedList
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 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?
class Node:
def __init__(self, value, next=None):
#!/usr/bin/env python
# Zero matrix
# Write an algorithm such that if an element in a MxN matrix is 0, its entire row and column are set
# to 0
import copy
class ZeroMatrix:
def __init__(self, matrix):
@humbroll
humbroll / string_rotation_1_9.py
Created August 7, 2016 05:30
String Roatation
#!/usr/bin/env python
# String Roatation
# 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")
class StringRotation:
def __init__(self, str_val):
self.str_val = str_val
@humbroll
humbroll / rotate_matrix_1_7.py
Last active August 13, 2016 21:31
Rotate Matrix
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Rotate Matrix
# Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes,
# write a method to rotate the image by 90 degrees. Can you do this in place?
import copy
class RotateMatrix:
@humbroll
humbroll / string_compression_1_6.py
Created August 7, 2016 05:29
String Compression
#!/usr/bin/env python
# String Compression
# Implement a method to perform basic string compression using the counts of repeated characters.
# For example, the string aabcccccaaa would become a2b1c5a3. If the "compressed" string would not
# become smaller than the original string, your method should return the original string.
# You can assume the string has only uppercase and lowercase letters(a-z)
import sys
@humbroll
humbroll / Detect and Remove Loop in a Linked List
Created July 31, 2016 04:37
Detect and Remove Loop in a Linked List
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Question.
# Detect and Remove Loop in a Linked List
# Write a function detectAndRemoveLoop() that checks whether a given Linked List contains loop and
# if loop is present then removes the loop and returns true. And if the list doesn’t contain loop
# then returns false. Below diagram shows a linked list with a loop. detectAndRemoveLoop() must change
# the below list to 1->2->3->4->5->NULL.
@humbroll
humbroll / top_twenty_frequent_words.rb
Created March 2, 2015 02:05
top_twenty_frequent_words
# Download this file - The Adventures of Sherlock Holmes
# http://www.gutenberg.org/cache/epub/1661/pg1661.txt
#
# Write a program to print the 20 most frequent words in the document, in
# descending order,
# with counts. Output format looks like:
#
# 9213 the
# 3223 i
def top_twenty_frequent_words(text)