210 likes | 335 Vues
This guide focuses on essential concepts in algorithm analysis for tech interviews, specifically Big O Notation and data structures like arrays, linked lists, stacks, and queues. Understanding Big O helps simplify the analysis of algorithms by quantifying time and space complexity. The content covers performance questions related to arrays and linked lists, including insertion and deletion operations, as well as practical examples such as reversing a linked list and merging sorted lists. Familiarity with these topics is vital for success in technical interviews.
E N D
Topics • Big OH Notation • Basics – to cover simple interviews. • Arrays • Question • List • Question • Stacks • Question • Queues • Question
BIG OH • Goal simplify Analysis of Algorithms by removing un wanted information • The basic goal is to understand • The limitations of a program • TIME • MEMORY • TIME • How much time does my program take as the input size increases? • Memory • How much memory will my program take as input size increases?
BIG O What is the BIG O? for (inti=0;i<n;i++ ){ for (int j=0;j<n;j++){ print “hello”; } print “Hello” } • How many Hellos will we print • (N^2) + 1 • When N =1 the last hello matters as it is 50% of the answer. • When N =1000 the last hello does not matter as it is .00001% of the answer. • Big OH implies what matters for large numbers - N^2
What is the Big O for (inti=0;i<n;i++ ){ for (inti=0;i<n;i++ ){ for (int j=0;j<n;j++){ print “hello”; } print “Hello” }
Arrays • Contiguous location of memory. • [1,2,3,4,5,6] • In Java • int [] a = new int[20]; always allocated on heap. • In C#,C++,Java values types are allocated in stack. • Typical JVM stack memory is 64KB. • .Netstack size 1M • Great when you know the number of elements you want to store. • You can pre allocate the memory and fill the spots. • ADD • If you know the index a[0] • If your array was 1000 – a[99] – still 1 operation • REMOVE( index of I) • A[i] = 0 • REMOVE( VALUE ) • Search for that N comparisons o(N)
Question on Array Find 2 numbers in an array that add up to a given number? Microsoft : Arrange the numbers in an array in alternating order. For example if the array is [a1, a2, a3, a4.. ]arrange the array such that b1<=b2>=b3<=b4 and so on. Sampe Input: 3 5 7 8 4 9 Sample Output: 3 < 5 > 4 < 8 >7 < 9
Linked List • How do we deal with situation when we don’t know the length of the elements we want to store. • Arrays[] – we need to know the length because the system allocates contiguous. • New Data structure Node{ int value; Node node; }
Linked List • Linked list • How do we move in the linked list? public void print(){ Node current = head; while (node.next != null){ System.out.println(node.value); }
Get the nth element ? public Node get(int n){ Node n; for (inti=0;i<n;i++){ if (node == null){ return null; } node = node.next; } return node; }
Linked List • Add to a linked list 1. Get the location to insert. 2. Insert. public void add(intn,int location){ PLEASE IMPLEMENT THE CODE. } Lets compare : • ADD • Since we don’t know the index – we have to traverse and then add to the end. O(N). • IF we decided to add to the head O(1). • REMOVE( index of I) • Travel to the index (i) and the remove O(N). • Compared to Array – O(1) • REMOVE( VALUE ) • Search for that N comparisons O(N). • Compared to Array O(N).
Linked List FIND INDEX OF SIZE
Variations Of LinkedList • Single • HEADNEXTNEXT • Double HEAD NEXT NEXT
Questions Reverse a linked list ( very common interview question) Given an integer linked list of which both first half and second half are sorted independently. Write a function to merge the two parts to create one single sorted linked list in place [do not use any extra]space]. How will you find a loop in a linked list. e.g. if the 4th node of the list is pointing back to the 2nd node (for a list of size 6), then it will be in a loop; how will you find this node?
Stacks & Queues • Using Arrays and Linked List • People decided to come up with Stacks and Queues. • Limited functionality • Only access to first element • Stack – first element is LAST entered. • Queue First element is FIRST entered. • Typical usage • Processing postfix expression evaluation • 2 3 *
Stack POP PEEK PUSH EMPTY
Queue ENQUEUE DEQUEUE PEEK
Stack In Action • Are the opening and closing brackets in the right order.
Question • Implement a circular queue using arrays of a given size? • enqueue() • dequeue()
Queues BFS Tree Traversal.