I'd also add in a discussion of graph search, because that's the one that always gets me on technical screens.

Graph Search

The basic algorithm of graph search is

Keep a list of nodes to search, initialized with a starter node. Optionally, keep a list of nodes you have visited.

For each node in that list, search that node.

If you've found the target, return.

Otherwise, mark that node as visited and add that node's neighbors to the list to search.

If you have run out of nodes to search, return an error.

Given a starting point (xStart, yStart) on a chessboard of size N * N, and an ending point (xEnd, yEnd), what is the minimum number of moves for a chess knight to get from the start to the end?

functionminKnightMoves(n,xStart,yStart,xEnd,yEnd){// Initialize chessboard. The value of each entry represents the number of moves to get there, as well as indicating whether we've visited that node.constchessboard=[]for(letii=0;ii<n;ii++){chessboard[ii]=[];for(letjj=0;jj<n;jj++){chessboard[ii][jj]=0;}}// List of nodes to search. Using array with [x, y] to represent points.// We want to do breadth-first search, so we're using a FIFO queue.// Depth-first search would require a LIFO stack. constnodeList=[[xStart,yStart]];// Search through each node.while(nodeList.length>0){constcurrNode=nodeList.pop();constcurrX=currNode[0];constcurrY=currNode[1];// If we've found the target, return the resultif(currX==xEnd&&currY==yEnd){returnchessboard[currX][currY];}// Search this node's neighborsif((currX+2<n&&currX+2>=0)&&(currY+1>=0&&currY+1<n)&&(chessboard[currX+2][currY+1]==0)){chessboard[currX+2][currY+1]=chessboard[currX][currY]+1;if(currX+2==xEnd&&currY+1==yEnd){returnchessboard[currX+2][currY+1];}else{nodeList.push([currX+2,currY+1]);}}// Repeat the above for all 8 possible knight's moves from this node.}// We've searched every possible node and haven't found it. return-1;}

We're a place where coders share, stay up-to-date and grow their careers.

I'd also add in a discussion of graph search, because that's the one that always gets me on technical screens.

## Graph Search

The basic algorithm of graph search is

Given a starting point (xStart, yStart) on a chessboard of size N * N, and an ending point (xEnd, yEnd), what is the minimum number of moves for a chess knight to get from the start to the end?