Skip to content

Instantly share code, notes, and snippets.

View kuntalchandra's full-sized avatar
🎯
Focusing

Kuntal Chandra kuntalchandra

🎯
Focusing
View GitHub Profile
@kuntalchandra
kuntalchandra / sum_root_to_leaf_binary_numbers.py
Created September 8, 2020 12:51
Sum of Root To Leaf Binary Numbers
"""
Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the
most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary,
which is 13.
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
Return the sum of these numbers.
@kuntalchandra
kuntalchandra / moving_average_data_stream.py
Created September 8, 2020 12:50
Moving Average from Data Stream
"""
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3
@kuntalchandra
kuntalchandra / word_pattern.py
Created September 7, 2020 09:28
Word Pattern
"""
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Example 1:
Input: pattern = "abba", str = "dog cat cat dog"
Output: true
Example 2:
@kuntalchandra
kuntalchandra / all_elements_two_bst.py
Created September 5, 2020 09:02
All Elements in Two Binary Search Trees
"""
Given two binary search trees root1 and root2.
Return a list containing all the integers from both trees sorted in ascending order.
Example 1:
Input: root1 = [2,1,4], root2 = [1,0,3]
Output: [0,1,1,2,3,4]
@kuntalchandra
kuntalchandra / contains_duplicate_iii.py
Created September 4, 2020 11:44
Contains Duplicate III
"""
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the
absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3, t = 0
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1, t = 2
@kuntalchandra
kuntalchandra / partition_labels.py
Created September 4, 2020 11:42
Partition Labels
"""
A string S of lowercase English letters is given. We want to partition this string into as many parts as possible so
that each letter appears in at most one part, and return a list of integers representing the size of these parts.
Example 1:
Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
@kuntalchandra
kuntalchandra / datediff.py
Created September 2, 2020 07:32
Find the difference between two dates
"""
Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
Example 1:
Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1
Example 2:
@kuntalchandra
kuntalchandra / fraction_recurring_decimal.py
Created September 2, 2020 07:30
Fraction to Recurring Decimal
"""
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
If multiple answers are possible, just return any of them.
Example 1:
Input: numerator = 1, denominator = 2
@kuntalchandra
kuntalchandra / largest_time_given_digits.py
Created September 2, 2020 07:30
Largest Time for Given Digits
"""
Given an array of 4 digits, return the largest 24 hour time that can be made.
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has
elapsed since midnight.
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
Example 1:
@kuntalchandra
kuntalchandra / above_average_subarrays.py
Created September 1, 2020 02:41
Above-Average Subarrays
"""
You are given an array A containing N integers. Your task is to find all subarrays whose average sum is greater than
the average sum of the remaining array elements. You must return the start and end index of each subarray in sorted
order.
A subarray that starts at position L1 and ends at position R1 comes before a subarray that starts at L2 and ends at R2
if L1 < L2, or if L1 = L2 and R1 ≤ R2.
Note that we'll define the average sum of an empty array to be 0, and we'll define the indices of the array (for the
purpose of output) to be 1 through N. A subarray that contains a single element will have L1 = R1.
Signature