21
Seattle Tech Interview

Seattle Tech Interview

  • Upload
    dena

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

Seattle Tech Interview. Topics. Big OH Notation Basics – to cover simple interviews. Arrays Question List Question Stacks Question Queues Question. BIG OH. Goal s implify Analysis of Algorithms by removing un wanted information The basic goal is to understand - PowerPoint PPT Presentation

Citation preview

Page 1: Seattle Tech Interview

Seattle Tech Interview

Page 2: Seattle Tech Interview

Topics

Big OH Notation Basics – to cover simple interviews.

Arrays Question

List Question

Stacks Question

Queues Question

Page 3: Seattle Tech Interview

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?

Page 4: Seattle Tech Interview

BIG O

What is the BIG O?

for (int i=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

Page 5: Seattle Tech Interview

What is the Big O

for (int i=0;i<n;i++ ){for (int i=0;i<n;i++ ){ for (int j=0;j<n;j++){

print “hello”; }print “Hello”

}

Page 6: Seattle Tech Interview

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. .Net stack 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)

Page 7: Seattle Tech Interview

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

Page 8: Seattle Tech Interview

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 structureNode{ int value; Node node;}

Page 9: Seattle Tech Interview

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);}

Page 10: Seattle Tech Interview

Get the nth element ?public Node get(int n){

Node n;

for (int i=0;i<n;i++){

if (node == null){ return null;

} node = node.next;

}

return node;

}

Page 11: Seattle Tech Interview

Linked List Add to a linked list

1. Get the location to insert.2. Insert.public void add(int n,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).

Page 12: Seattle Tech Interview

Linked List

FIND INDEX OF SIZE

Page 13: Seattle Tech Interview

Variations Of LinkedList

Single HEADNEXTNEXT

DoubleHEAD NEXT NEXT

Page 14: Seattle Tech Interview

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?

Page 15: Seattle Tech Interview

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 *

Page 16: Seattle Tech Interview

Stack

POP PEEK PUSH EMPTY

Page 17: Seattle Tech Interview

Queue

ENQUEUE DEQUEUE PEEK

Page 18: Seattle Tech Interview

Stack In Action

Are the opening and closing brackets in the right order.

Page 19: Seattle Tech Interview

Question

Implement a circular queue using arrays of a given size? enqueue() dequeue()

Page 20: Seattle Tech Interview

Queues

BFS Tree Traversal.

Page 21: Seattle Tech Interview

Question