Skip to content

Instantly share code, notes, and snippets.

View eric100lin's full-sized avatar

Eric Lin (Tzu Hsiang Lin) eric100lin

  • Taiwan
  • 16:23 (UTC +08:00)
View GitHub Profile
'''
Hi, here's your problem today. This problem was recently asked by Uber:
Given a list of numbers, find if there exists a pythagorean triplet in that list. A pythagorean triplet is 3 variables a, b, c where a^2 + b^2 = c^2
Example:
Input: [3, 5, 12, 5, 13]
Output: True
Here, 5^2 + 12^2 = 13^2.
'''
'''
Hi, here's your problem today. This problem was recently asked by Twitter:
Implement a class for a stack that supports all the regular functions (push, pop) and
an additional function of max() which returns the maximum element in the stack
(return None if the stack is empty). Each method should run in constant time.
'''
class MaxStack:
def __init__(self):
self.elements = []
'''
Hi, here's your problem today. This problem was recently asked by Apple:
Given an integer k and a binary search tree, find the floor (less than or equal to) of k, and the ceiling (larger than or equal to) of k. If either does not exist, then print them as None.
'''
class Node:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
'''
Hi, here's your problem today. This problem was recently asked by Microsoft:
You are given an array of integers in an arbitrary order. Return whether or not it is possible to make the array non-decreasing by modifying at most 1 element to any value.
We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
Example:
[13, 4, 7] should return true, since we can modify 13 to any value 4 or less, to make it non-decreasing.
'''
Hi, here's your problem today. This problem was recently asked by Facebook:
Given a list of numbers, where every number shows up twice except for one number, find that one number.
Example:
Input: [4, 3, 2, 4, 1, 3, 2]
Output: 1
'''
def singleNumber(nums):
'''
Hi, here's your problem today. This problem was recently asked by Google:
Given a list of numbers with only 3 unique numbers (1, 2, 3), sort the list in O(n) time.
Example 1:
Input: [3, 3, 2, 1, 3, 2, 1]
Output: [1, 1, 2, 2, 3, 3, 3]
'''
def sortNums(nums):
'''
Hi, here's your problem today. This problem was recently asked by AirBNB:
Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element, x. Return -1 if the target is not found.
Example:
Input: A = [1,3,3,5,7,8,9,9,9,15], target = 9
Output: [6,8]
Input: A = [100, 150, 150, 153], target = 150
'''
Hi, here's your problem today. This problem was recently asked by Microsoft:
Given a string, find the length of the longest substring without repeating characters.
Can you find a solution in linear time?
'''
class Solution:
def lengthOfLongestSubstring(self, s):
seen = set()
'''
Hi, here's your problem today. This problem was recently asked by Microsoft:
You are given two linked-lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
'''
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def rotateRight(head, k):
if head is None or k==0:
return head
node = head