Skip to content

Instantly share code, notes, and snippets.

View amarjitdhillon's full-sized avatar

Amarjit Singh Dhillon amarjitdhillon

View GitHub Profile
amarjitdhillon /
Created February 12, 2022 08:54
CKAD exam cheatsheet
kubectl set image pod podname nginx=nginx:1.15-alpine
kubectl edit pod podName
# Create the nginx pod with version 1.17.4 and expose it on port 80
kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=
kubectl run nginx-pod --image=nginx:alpine # will create a pod with name nginx-pod
# add the command in the pod
command: ['sleep', '5000']
amarjitdhillon /
Created February 12, 2022 08:07
Range Addition II
class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
for r,c in ops:
# expand and shrink the matrix size (bottom-right boundry of matrix) based on the current operation
m = min(m,r)
n = min(c,n)
# total number of elements will be multiplication of bottom and right boundry asssming left and top boundry is 0,0
return m*n
amarjitdhillon /
Created February 12, 2022 06:44
Range Addition
class Solution:
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
# Initialize an array of length+1 with all values as 0
arr = [0] * (length+1)
# Iterate over all the given ranges
for start, end, value in updates:
Just update the start index with +val and end+1 with -val as we need to see the effect till end
We will use the prefix sum to retrive the actual sum, later
amarjitdhillon /
Created February 12, 2022 05:21
Coin Change
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
# Create a dp array for 0 to amount (inclusive) with the max values
dp = [float('inf')] * (amount+1)
# Initialize the value of the dp array
dp[0] = 0
# Let's compute dp array for all values from 1 to amount
for i in range(1, amount+1):
amarjitdhillon /
Created February 11, 2022 23:14
Number of Islands
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
num_rows,num_cols,num_islands = len(grid), len(grid[0]),0
visited = set()
def findIsland(row,col):
# add this cell to the visited set
# add left, right, top and down neighbours of this cell respectively. Here the first element is row id and second one is the column id
amarjitdhillon /
Created February 11, 2022 07:46
Merge Intervals
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
# sort the intervals by the start value, in case they are not sorted by default
intervals.sort(key = lambda x: x[0])
# edge case. If num elements is one, then return the same list
if len(intervals) == 1:
return intervals
amarjitdhillon /
Created February 11, 2022 01:23
The kth Factor of n
class Solution:
def kthFactor(self, n: int, k: int) -> int:
# counter represent the i^th factor of the number found so far
counter = 0
# go over the number from 1 to n as we need to divide the n by this number to find if that number is a factor
for i in range(1, n+1):
if n%i == 0:
amarjitdhillon /
Created February 10, 2022 23:45
Analyze User Website Visit Pattern
class Solution:
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
# zip the time, user and website to one list
tuw = list(zip(timestamp,username,website))
# we need to sort them by time --> username --> website
sorted_tuw = sorted(tuw)
# We will polulate a user history hashmap of various pages visited. The hashmap is of type {user: [pages]}
amarjitdhillon /
Created February 10, 2022 17:10
Design Parking System
class ParkingSystem:
# initialize the parking object
def __init__(self, big: int, medium: int, small: int):
# to hold various types of parking we are using a dict in constructor
self.lot = {}
# add the parking counters in this lot dict
self.lot[1] = big
self.lot[2] = medium
self.lot[3] = small
amarjitdhillon /
Created February 10, 2022 00:10
Word Pattern
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
hm = {}
# we need to split the s over the space(" ")
s = s.split(" ")
# if the lengths of pattern and s are not same then return False
if len(s) != len(pattern):
return False