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
#!/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. |
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
#!/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] |
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
#!/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): |
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
#!/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): |
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
#!/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): |
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
#!/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 |
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
#!/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: |
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
#!/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 |
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
#!/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. |
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
# 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) |
NewerOlder