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 / 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
@kuntalchandra
kuntalchandra / answer_a_query.py
Created September 1, 2020 02:40
Answer a Query
"""
Imagine a length-N array of booleans, initially all false. Over time, some values are set to true, and at various points
in time you would like to find the location of the nearest true to the right of given indices.
You will receive Q queries, each of which has a type and a value. SET queries have type = 1 and GET queries have
type = 2.
When you receive a SET query, the value of the query denotes an index in the array that is set to true. Note that these
indices start at 1. When you receive a GET query, you must return the smallest index that contains a true value that is
greater than or equal to the given index, or -1 if no such index exists.
Signature
@kuntalchandra
kuntalchandra / delete_node.py
Created September 1, 2020 02:39
Delete Node in a BST
"""
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node
reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Search for a node to remove.
If the node is found, delete the node.
Note: Time complexity should be O(height of tree).
@kuntalchandra
kuntalchandra / balance_brackets.py
Created September 1, 2020 02:35
Balance Brackets
"""
A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].
We consider two brackets to be matching if the first element is an open-bracket, e.g., (, {, or [, and the second
bracket is a close-bracket of the same type, e.g., ( and ), [ and ], and { and } are the only pairs of matching brackets
Furthermore, a sequence of brackets is said to be balanced if the following conditions are met:
The sequence is empty, or
The sequence is composed of two, non-empty, sequences both of which are balanced, or
The first and last brackets of the sequence are matching, and the portion of the sequence without the first and last
elements is balanced.
You are given a string of brackets. Your task is to determine whether each sequence of brackets is balanced. If a
@kuntalchandra
kuntalchandra / sum_left_leaves.py
Created August 30, 2020 04:35
Sum of Left Leaves
"""
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
@kuntalchandra
kuntalchandra / binary_tree_left_side_view.py
Created August 30, 2020 04:11
Number of Visible Nodes from Left
"""
There is a binary tree with N nodes. You are viewing the tree from its left side and can see only the leftmost nodes
at each level. Return the number of visible nodes.
Note: You can see only the leftmost nodes, but that doesn't mean they have to be left nodes. The leftmost node at a
level could be a right node.
Signature
int visibleNodes(Node root) {
Input
@kuntalchandra
kuntalchandra / maze.py
Created August 23, 2020 02:47
The Maze
"""
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left
or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the
borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
Example 1:
Input 1: a maze represented by a 2D array
0 0 1 0 0