Skip to content

Instantly share code, notes, and snippets.

View rishi93's full-sized avatar

Rajarishi Devarajan rishi93

View GitHub Profile
@rishi93
rishi93 / day-20.py
Created December 12, 2019 11:28
Daily Coding Problem - 20
"""
This problem was asked by Google.
Given two singly linked lists that intersect at some point, find the intersecting node. The lists are non-cyclical.
For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.
In this example, assume nodes with the same value are the exact same node objects.
Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.
@rishi93
rishi93 / day-11.py
Last active December 7, 2019 15:27
Daily Coding Problem - 11
"""
This problem was asked by Twitter.
Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix.
For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal].
Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries.
"""
class Node:
@rishi93
rishi93 / day-13.py
Last active March 4, 2022 12:49
Daily Coding Problem - 13
"""
This problem was asked by Amazon.
Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters.
For example, given s = "abcba" and k = 2, the longest substring with k distinct characters is "bcb".
"""
def longestSubstring(string, k):
start, end = 0, k
max_len = k
@rishi93
rishi93 / day-9.py
Created December 5, 2019 14:24
Daily Coding Problem - 9
"""
This problem was asked by Airbnb.
Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.
For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5] should return 10, since we pick 5 and 5.
Follow-up: Can you do this in O(N) time and constant space?
"""
def largestSum(arr):
@rishi93
rishi93 / day-8.py
Created December 5, 2019 12:20
Daily Coding Problem - 8
"""
This problem was asked by Google.
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 5 unival subtrees:
0
@rishi93
rishi93 / day-12.py
Created December 4, 2019 16:41
Daily Coding Problem - 12
"""
This problem was asked by Amazon.
There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters.
For example, if N is 4, then there are 5 unique ways:
1, 1, 1, 1
2, 1, 1
1, 2, 1
@rishi93
rishi93 / day-7.py
Last active December 4, 2019 16:11
Daily Coding Problem - 7
"""
This problem was asked by Facebook.
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.
For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'.
You can assume that the messages are decodable. For example, '001' is not allowed.
"""
def count(string):
n = len(string)
return _count(string, n, 0)
@rishi93
rishi93 / counter_tb.vhd
Last active December 6, 2018 14:11
A simple testbench for the counter in VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity special_counter_tb is
end entity;
architecture tb of special_counter_tb is
component special_counter is
@rishi93
rishi93 / counter.vhd
Created December 6, 2018 13:54
A simple example in VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity special_counter is
port(
clk : in std_logic;
rst : in std_logic;
output : out std_logic_vector(0 to 5)
@rishi93
rishi93 / test4.py
Last active November 6, 2018 08:43
Animated 3D plot in Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib.animation import FuncAnimation
# Create the figure and the axes
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d', title = '3D curve animation')
# Initialize the axes