DataStructures and Algorithms Notes
DataStructures and Algorithms Notes
>
- It doesn't matter how many interviews you fail or going to fail,
all that matter is the one
you succeed !
Resume
~
>
- Resume's are
way to
get interviews !
-> One
page (only)
- job description
>
- Relevant Skills (keywords)
>
- personalized >
-
different resume for different companies !
>
- online links !
>
- By looking at your resume people should know what you're applying to
>
- Do not use buzzwords - team leader in this , that etc .
>
- use action words
Email
~
-
Networking is the key !
find Jos
~re
To
-
Hackernews
-
Angelist (start-up)
>
- we work remotely
Hired
3
>
-
gives You quiz to help companies
>
- Triblebyte hire developers .
>
- Whitetruffle
BOB ? an
Even if
interview
you
is
do
a
not
key !
pass it,
getting
>
- lob description is what
type will
doing What jobs
of job
you be not
type of
you have
Scalable
>
- -
Big D
#
onlogn)
Or
-
&
oclogn) ,
O(1)
-
I "
Elements good
3.
for (in + i = 0 ; i < num ; i ++ ) 2
This will be
if (i = = 5)9
ar
system .
Out . Printin ("Hello 1)
3 time
3
public static void printElem(intarr[])[ This is
going to
be
system . Out - Printin (arr(]); OC]
=
3 G constant time
-
~Rules
>
-
Rule 1 :
Big O refers to worst case
>
-
Rule 2 : Remove constants ! ocyn +
p) = o(n)
3
for /Box item : boxes)[
On)
system. Out Printin (item) ;
>
-
3
for (Box item : boxes 2) E
OcMs
System Out-printin .
(item:
3
↑
Rule 4 : Drop non dominant terms
3
i < arr length ; i + + ) &
for (inti 0 ;
.
OCH)
>
Printin (arr [i]);
system . Out no
dominant
.
3 ochtn2)
3
= or (in + i = 0 ; i < arr length ; i + + 15
O(n2
.
3
public void boo (n) E
3 Space P
boo /[1 ,
2, 3 , 4 , 5]) ; +What is the space complexity of this
function ? O(1) -
no adding in memory !
let hiArray =
[J ; //0()
mmm
to solve problems ?
>
-
interview is not about how well have memorized Data structure
you
& Algorithms ! (Biggest mistake ! (
~
orithms
structur as
Data
is
>
- Trees T Dynamic Programming
↑ StackS - Tries >
- BFS + DfS (searching)
-> Queues - Graphs >
- Recursion
>
- Linked Lists + Hash Tables
it speci-
I
Each data structure is good for the thing that
alized to do !
>
- How to build a data structure ?
a
>
- How to use one ? A most important ! -
·
rea
-
computer
lot faster
Y information
When we turn off
you do not lose
we lose the data
in RAM
E
insertion
common
A
operation >
- d election How do we access
that is going
- traversal the data we have ?
to be performed
on data searching
structures
sorting
Access
rays
organizes items
sequentially
>
-
>
- If all you need is storing items and iterate over it one by One then
int[] arr = 1 ,
2 ,
3 , 43; 100kup : 0(1)
delete : On
Static Arrays : fixed in size , you must specify the length beforehand and you cannot exceed the
Dynamic Arrays : When you want to add item to the dynamic array which is full, dynamic
array !
copy the array to
bigger array in size
inta = 1;
in+ b = 1;
System . Out .
Printin (a == b) ; // true
Student Std3 =
new Student ("Bob") ;
System. Out .
Printin)Std1 == Stdz); is true
System . Out .
Printin (Std1 == Std3) ; /l false
* you should look each string questions as array questions ! Because at the end ,
characters .
strings are array of
nuerview
~
Questions
>
- Reverse a string
String reversed =
"
;
for (i = Str i )5
·
length -
1; < =
0 ; i
--
3
return reversed;
3
>
- sorted
Merge Arrays
int index z = 0;
caseswhichshouldbechecked. .
int /len] sorted ;
-
index 2 ++;
for
checking purposes !
3
3
return sorted ;
3
When you should use an Array ? Why you shouldn't use Arrays
3
>
- fast look ups - Slow inserts because we need
cepy the
to push or
1
>
- fast push , pop slow deletes entire array in Worste !
insert +
OCI) Main problems With Hash Table
~
n
table implementation
~
+ his .
data = new Array (size) ;
3
Public void Set /key value) ,
(S key ,
value 3);
3
I curr) <
if // if curr's length ! =
0
for (inti = 0
; i < curr size() ;
. i ++ /
if (curr[o](0] = = key) E
return curr[O](17 ;
3
Y
3
return 0;
return Keylist;
3
Interview Question You need to use hash table
to optimize
Given the first character naive method !
an array ,
tell me recurring -
code :
u
Static Array - has a certain size, to be able to add element to an array we need to
Dynamic Array - double its size When the limit has been passed !
Y
generate double size array and copy each element into that array in worst
case
- Delete is same
hand deletion
->
Hashmaps on the other are great for insertion and but
they are
>
- Does this means that linked lists are the best data structures therefore we should
always
head
used them
? ↓
Fail
X prepend O(1)
littl
Append+ O(1)
100kup - On
linked List
my
head +ail
prepend - O(1)
nunerdatalnexin
datakexidets/next->
searching
nu Append OC through doubly
-
-
linked List
100kup >
- O(n(
a bit more
insert >
- On ( efficient
delete- on then linked
Downsides more memory ! vis + (0(EI)
althoug techically
OL1 is on I therefore we
>
- cannot be iterated through back
cons -
lost forever)
(if we lest head reference
, list can be
-
- Effective search
d
-
more operation
-
complex !
second
>
-
Reversing linked list first
L J temp
public Node
While (second) 9
Node temp = second next;
first = second
,
second-temp ;
3
Node last = This head;
3
Linked List
we need to
Prol Cons - reversed
u -
Through
>
- fast insertion + Slow lookup List !
As
>
- Last in first out (LIFO)
100kup >
- O(n)
Push >
- 011)
Pop-O(l)
Peeke 0(1)
mes
>
-
first in first out (fIfO)
100kup 1 On
enqueue >
- Ol
dequeue - all
peek >
- OC)
Since there will be no need to deuble the size when array is full.
to linked
: Why would you want implement queues as an
dynamic Array or
List ?
>
-
implementing queues as an array wouldn't be
practical at all since to delete
the first item we need to switch places for all the others which takes (ocnl/runtime
what is program
?
-
-
Program alatememoryexecute crun s commands .
memory leak : Happen When I have unused memory /unused variable etc . ) that is
laying around
Stack Overflow
e
i
Seowed!
Call stack ge biga
and
bigger bigger until there
is no space left !
&
How this happen
function
predictable
T
synchronue programming - But it can get slow since it does everything one by one and
&
.
-
code Execute -② meout()
# this is going to execute now
(1) Printin
(
① -
out .
system
System. Out Printin (1); -
call stack
stac itemp y
③ timeout)) - wait I see is
&
.
new
or not
After that
eioalback
System. Out . Printf (4); to cana
-
goes
z queue
&
time out () ;
System. out .
Printf (6);
continuous check but let say 6
Event loop - Check if the callstack empty
if it is not going to chelk
Butput callback
5
Y
① -
single root node
root : I
d Parent
child :
:
234
13
67
↓J leaf : 2674
⑧ sibling : 23467
Ary
Tree
1 1
11) 1)
- 1
G at better node level 0 : 20 1
we have =
level 2 : 2 =
4
3 : 23 8
level =
height of - = logn
the tree = n +1
Great for searching , comparing things
-
Binary Search Tree >
- right child is bigger than the node
e
i
left child is smaller than the node
/ Yos lookup :
OClogn)
Average
33
insert O(logn)
case
· on Biggest
:
delete : O Clogn)
smallest
-you can have unbalanced binary trees which is similar to linked list which
takes
lookup : O(n( worst
Case !
insert : O(n)
*
dete : on
ne cons
con
M um
>
-
operations
most are >
- No Ocls operations
better than O(n)
- ordered
>
- flexible size
BinarySearchTree
usin
Node left ;
return nul ;
public Node (int datald 3
this Value . = data ; Node cur= this root
3
Node parent null
;
3
1 While (cur)[
Public Binary Tree &
③ G if luak sur. value)[
d
Node root
parent = cur ;
head-head left ;
parent right cur right;
,
=
.
Y
esses 3
head left = new Node;
.
Cont = false;
else if (cur right. left == nulls
3
.
3
if ( Parent = nuu)a
else if( head. value < val) E
if (nead right - nulls .
!
head-head right;
3
else
head. right = new Node;
3 cont = false;
else
system . Out . Printin ) "There is already" val) ;
+
3
public Node lookup (intral) [
right;
3
3
return nun;
3
AVL Tree
T
Balanced Binary Trees
- Tree
Red-Black
root-root
-
max heap
ordered
is the maximu a insert >
-
O(logn)
>
- value !
33 delete Ollogn)
72
>
-
XX
5 f
2 30
*
Only guarantee that binary heap is parent is (if max heap) childeren.
gives bigger then
Quelles Each
Priority -
element in queve has priority and elements with higher priority
~ served first.
(if it is high prierity queue)
& + here is
left to
an
right
erdet of
insertion
insertion . Better
/insert, delete
than O(n(
priority
Slow lookup
you know the
flexible size
fast insert
Tries >
- specialized tree used in searching
4
A 2 lookup : 0 (length of the word (
B ---
- ↓ &
I
R
E
S
I E
E I N
A
A
Ephs
·
Directed graph undirected Graph
·
>
-
go back you can
·
cunless there is two edge (
y 4
2 2
11 1
"
- -
*
7
implementation :
mu
this. to = node 2 ;
public void add vertex (Node node)5
3
Vertex list . add (node);
is
3
3
Algorithms
num
>
- Algorithms helps us to create actions using data structures .
3 What a
sorting
in
>
-
Algorithms
>
-
>
- Dynamic programming we
BFS + DFS (searching (
Recursion
Recursion -
when you define something in terms of itself
.
w
U Good if there are repeated subtasks
StackOverflow >
- Happens When function get called over & over again .
i . .
e
public void
StackOverflow (1j stackoverflow(s] SECASE !
public main()E
? &
This StackOverflow();
.
>
- this will create stack overflow
int count = 0
; Rule 1 : Identify the base case
pount-3525
if
-
- -
"done !
"
return ;
wonrecursive
- -
·
---
3
IMPORTANT
-
find factorial
return 1 ;
3
int answer = 1;
num--;
return answer;
3
acci
return O ;
return 1;
esse G
int answer = 0;
if (index = = 01 [
return o;
Y
else if (index == 119
return 1;
Y
elses
List <int > fibonacci = new ArrayList()();
fibonacci . Add (0) ;
fibonacci- add (1);
for (inti = 2
; i < index ; j ++ ) &
fibonacci - add (fibonacci . get (i-2) +
fibonacci get (i-1)) ;
return fibonacci .
get (index 1 ;
3
Anything you do recursively
Can be done iteratively .
Pro cons
~ ~
readability
>
-
Bread first search & depth first search , recursion is useful then
iterative approaches .
In
>
- serting cases recursion also better .
consider recursion .
if (cur :
cur=tree. root;
nuu)'s
=
3
if (cur data . == node , datal 5
return cur ;
3
else if (cur. data <node-data) E
eise E
return traverse (cur -
left);
3
3
return nun;
3
Eing
Why do we learn about sorting while we can use the ones already there ?
?
What is the issue With Sort)) -
Different implimentation in each programming languages
h
Do not sort as
you expect for different input types
So would need modification depending on the input you get
.
Bubble sort
-
ElementarySorting Insertion
-
Sort
Bubble Sort
- least efficient
-
Algorithms
Selection sort
wi
sor t
merge
155
-
318724 Complex Algorithms
sort
- Quick
65 bubble
(switch)
515631872 16721815)
·
53
Y
63 - Switch
u switch
code
u
(intiarrlength
int length =
for gth ; i + + 12
for (in + ( = 0
; /length ; j ++ 1 &
if ( arr .
get (3) > arr .
get ( + 1)[
int temp = arr .
get(j + 1) ;
3
3
length-- ;
3
return arr
;
3
tionor
~
e
· ·
5
int
-
length = arr .
length;
Int min ;
for , int i
= o; i < length ; i + + 12
min =
is
for (intj = 0 ; ) (length -
1
; c + 19
;
min = 1+ 1 ;
3
3
int temp = arr .
get(i);
arr . Set (i , arr .
get (min 1;
arr . Set /min , temp);
Y
return arr ;
Best case
O(n2)(
IISEBSISESS1715
>
-
performs very very well on small in put sizes .
for (in + i =
0 ; i < len ; it + S
arr[ij = temp ;
3
else3
# or (in+ c = 0 ; +(ijj + + )5
arr [J] =
temp;
Average case
O(n2S
Worst case O(n2)
space complexity O(1)
MergeSert use divide and conquer technic a
Age
sort
6 5
t 31 87 24
DIVIDE
L Y 1 d - L x
I 872 Y
6 S 3
Y [ -4 -X
I
↓
56 1 .3 78 2 Y
- L Y L
Conquer
1356 2/478
- I
1234 5678
3 for (inti i ++ S
<i
= 0 ;
left-add (array .
get (i) ;
right .
add Carray. get (i + len/2) ;
3
length ;
int lenr =
right. length;
int left Num = left get (0) ;
-
int rightNum =
right-get(o) ;
boolean cont = true
;
int index 1 0; =
int index R = 0 ;
while (cont);
if (index ( < lenL && index &ClenR 15
index R + +j
Y
elses
I
put -
Output
peach apple
raw peach
it is an top of spark
Spork Spark
Quick Sort
wir
nothing needs to change
&
&
dgsau/create zoacetatechange
3)
is
with
be on left
I
31112/5Tu10/9/s/z 2/1/2
Nothing ~
divice the
a
3 (1 (2 (4/5/8/9/s/7
rightand
continue doing a
ad
Best case ocniogn)
Average case Oinlogn)
Worst case & (n2) - When Pivot is either smallest or biggest
Space complexity Ollogn) - Better than
Merge sert
sort is best ?
h
Easy to implement
Cy
>
-
Since it is it used
very simple might fer
educational purposes
selection sort
- same as bubble sort !
If
you are considering space complexity
Non-comparison sort
eme
us
Radix
Sort only work With fixed length integers
counting Sort
Searching
nu
Linear
Search-searching in an input one by one until finding the object
.
-
Binary search
<
/4/0(9(12/34/45245 ↓ so
: =
uu is not
u4
Breadth-first search
Tree & S
But what if we want to visit all the nodes + Graph - Depth-first search
Traversal
Breadth-first Search - we need to track every node and its children in order
~
meaning that it uses
memory (
G
②X ③
① 2 ①
1 # 34 45
first Search
& - Not
necessarly need to remember each node and its children in order
③ 34 45
>
-
eath-first ee-first
shortest
search
path
us
>
- Does
search
path exist ?
>
- Closet Nodes >
- Less memory
tree then breadth-first search better lower layers of the tree the
#1 solution is BFS
If you how not far from tree : is better
#2 If the tree is very deep and solution are rare : BFS because
DFS take)
very long time to search in depth. Although with Bfs
# 3 If the tree is very wide : Then BFS takes a long time and memory the
DFS SO DFS .
Node curr = this root; public List <Integer] breaden first Search Reg (List (Integer> list , Queue queued
(curr) ;
cur =
queue pop();
.
queue. push
list add (cur);
While (queue -
size ( > 01 &
if ( cur left ! = null E
curr = queue Pop() ;
-
if
.
queue .
push (curr left);
. queue .
Push /Cur .
right) ;
3 3
if return breadth first Search Reg) queue , list);
curr ·
right != null &
return list;
3
in order
>
-
three way that
first Search -
Depth pre-order
You can impliment
umm post-order
① ⑦
9 9 9
[ 6 ② ③
04 20
=
20
+ ⑦0 " +
20
⑤
16 150 170 ↓ 6 150 170 ↓ 6 15 178
in -
order Pre-order Post-order
traversal traversal traversal
in order
u
3
list-add / cur) ;
if
( cur right ! = nuul &
+ raverse In order (Cur right , list) ;
Y
return list;
3
public List /Integer > Afspreorder () [
Ey
public List /Integer) traverse preorder (Node cur , List Integer > list) &
list-add (cur) ;
3
return list ;
3
if ( kar-right ! -null
return list;
3
Algorithms & Bellman-Ford
~
Stra
To
figure out shortest path problem
Because breadth-first search will not look for weight of the edge !
namic
programminga optimization
~
one
memorization
Caching used to speed
~ programs
up our
(
Storing values from the function to not run a function that
fibonacci
u s e
a
Recup
&
fibonacci
& Dynamic
with
programing
recursion
Divided
ization
Dynamic programming
3
public int fibonacci (n)[
3
fibonacci (n-1) + fibonacci (n-2);
return
3
using dynamic programming though decrease O(n) ! cusing
, we could the time from On to
memorization (
③ Are there
any repetitive sub-problems ?
Dynamic fibonacci
- programming of
if (GS2)[
this fibonaccilist[n] = h ;
3
else
this fibonacci List [n] = this fibonaccilist[n-1] +
this fibonaccilist[n-2] ;
return fibonaccilist[n];
3
3
3
Technical Questions
un
set
#
issue
Thinking
:
that this is your only chance
ONLY CHANCE
① & here
must be
story
prepared
-
-
O
Challenge
f about very difficult
& problem and your solution
>
-
Technical
J ③ to overcome it
.
Leadership
Difficult protect ② -
① Even tho h
i
- or of women
group
-
②
In our school there was an Al computation of Vestel and me
Top in
equal parts and ask each and every one of them what do they
want to ob and gave the Lob -
SAND
OUT !
write your own
why us ?
- Make the interviewer feel special . Make them feel that this is
the occurred at
problems your Tob .
&
* Tell me about Problem how you solved it
Situation -
SAR Y
method
Result
How you solved
is
scaling ,
performance
>
- snow now you improved it .
Any Question for me ?
out !
Key points-stand
>
- focus on them , not company
mention mentioned
something they
-
Exi
-
SECRET WEAPONS
-
Simplicity over complexity
>
- premature Optimization is the root of all evil
- Overall
goal , myopic !
not (focus on what is important not what
-
- Not
complaining about client/code etc.
Handling Rejection -
After receiving rejection do not
gave
up ! It doesn't mean you lack of skills .
Negotiation
What
·
salary you would expect
+ Since I know
average software
i is a
big decision for me at the mement etc .
·
Always negotiate
· Be positive
· Have stakes 1
show company that
you have other options !
Handling an offer
>
- Don't end the conversation by saying this sound good. This close the doer
for negotiation !
>
-
say you need to think this offer since there are other companies that you
for
are
waiting
>
- BE POSITIVE
>
- ASK FOR TIME
>
- AFTER YOU DECIDE WHAT ARE YOU GRING TO CONTINUE WITH ,
LET OTHER
COMPANIES know !
Handling Multiple Offers
⑦ IS there an offer that you feel you are under qualified for ?
Go to that
company
② long term
growth potential
Then yes
-
⑤ is
your decision based on desperation ?
Getting a Raise
St
1 Step : Be a good employee
-
Before Starting , know your career path in advance !
A rais.
-