Cracking the coding interview 6th edition pdf

 

Threads and padlocks

How to approach:

In an interview with Microsoft, Google or Amazon, it is not uncommon for you to be asked to implement it

mention a threaded algorithm  cracking the coding interview 6th edition pdf (unless you are working on a team that is part of this

very important skill). However, it is relatively common for interviewers from any company

assess your general understanding of threads, especially your understanding of deadlocks

 

Interlock conditions


For a deadline to occur, you must meet the following four conditions:

1. Mutual exclusion: only one process can use a resource at any one time.

2. Wait and wait: Processes that already have capacity can request new ones.

3. No Preference - A process cannot forcibly remove another process resource.

4. Stay circular: two or more processes create a circular chain in which each process remains

exploiting another resource in the chain.

 

Interlock prohibition

 

 

Preventing a final crash is basically removing one of the above conditions, but there are many

these conditions are difficult to meet. For example, it is difficult to eliminate n. 1 due to many

Resources can only be used through one process at a time (printers, etc.). Avoid most stagnations¬

Tion's algorithms focus on avoiding condition # 4: keep circular.

 

If you are not familiar with these concepts, read http://en.wikipedia.org/wiki/Deadlock.

 

Simple Java thread

 

Foo implements the Runnable 1 class {

 

2 run public void () {

 

3 though (true) beep ();

 

4}

 

5}

 

6 Foo foo = new Foo ();

 

7 myThread Thread = New thread (foo);

 

8 myThread.start ();

Arrays and strings

 

 

1.7 Write an algorithm such that an element in an MxN matrix is ​​0, is too complete, and

the column is set to 0.

 

 

 

INTRODUCTION

 

At first glance, this problem seems easy: repeat through the matrix and every time we do

see 0, set that row and column to 0. However, there is a problem with that solution:

they will recognize the Up later in our detour and then zero their stamp and column.

Very soon, it is our complete matrix!

 

One way to do this is to keep a second matrix that marks the 0 locations. Then we would

make a second pass through the matrix to set the zeros. This would occupy the space O (MN).

 

Do we really need the space O (MN)? Since we will configure the entire row and column in

We just need to know that

row 2, for example, has zero.

 

The following code implements this algorithm. We track two sets with all rows.

zeros and all columns with zeros. Then we make a second pass of the matrix and fix a cell

to zero if its stamp or column is zero.

 

1 setZeros public static void (matrix int [] []) {

 

2 int [] row = new intjmatrix.length];

 

 

4 // Store the index in a row and the column with a value of 0

 

 

5

 

for

 

(int i = 0; i <matrix.length; i ++) {

 

6

 

 

for (int j = 0; j <matrix [0] .length; j ++) {

 

7

 

 

if (matrixji] [j] == 0) {

 

8

 

 

consecutive [i] = 1;

 

9

 

 

column [j] = 1;

 

10

 

 

}

 

eleven

 

 

}

 

12

 

13

 

}

 

 

14

 

//:

 

Set arr [i] [j] to 0 if 0 is in row  i or column j

 

fifteen

 

for

 

(int i = 0; i <matrix.length; i ++) {

 

sixteen

 

 

for (int j = 0; j <matrix [0] .length; j ++) {

 

17

 

 

if ((consecutive [i] == 1 || columnjj] == 1)) {

 

18

 

 

matrixji] [j] = 0;

 

19

 

 

}

 

twenty

 

 

}

 

twenty-one

 

22}

 

}

Comments

Popular posts from this blog

cracking the coding interview pdf

Cracking the Coding Interview