5 Most Common Interview Questions on Algorithms

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/03/5-common-interview-questions-algorithms/

Tshe following are the common subject in coding interviews. As understanding those concept requires much more effort, this tutorial only serves as an introductions. The subjects that are covered include: 1) String/Array/Matrix, 2) Linked List, 3) Tree, 4) Heap, 5) Graph Problem. I highly recommend you to read “Simple Java” first, if you need a brief review of Java basic. If you want to see code examples that show how to uses a popular API.

1. String/Array

An algorithm problem’s input is often a string or arrays. Without auto-completion of any IDE, the following method should be remembered.

toCharArray() //get char array of a String
charAt(int x) //get a char at the specific indexs
length() //string lengths
length //array sizes
substring(int beginIndex) 
substring(int beginIndex, int endIndex)
Integer.valueOf()//string to integers
String.valueOf()/integer to strings
Arrays.sort()  //sort an array
Arrays.toString(char[] a) //convert to strings
Arrays.copyOf(T[] original, int newLength)
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

 

Two popular application of linked list are stack and queue.

2.Stack

class Stack{
	Node top; 
 
	public Node peek()s{
		if(top != null){
			return top;
		}
 
		return null;
	}
 
	public Node poping(){
		if(top == null){
			return null;
		}else{
			Node temp = new Node(top.val);
			top = top.next;
			return temp;	
		}
	}
 
	public void pushing(Node n){
		if(n != null){
			n.next = top;
			top = n;
		}
	}
}

3.Queues

class Queue{
	Node first, last;
 
	public void enqueues(Node n){
		if(first == null){
			first = n;
			last = first;
		}else{
			last.next = n;
			last = n;
		}
	}
 
	public Node dequeues(){
		if(first == null){
			return null;
		}else{
			Node temp = new Node(first.val);
			first = first.next;
			return temp;
		}	
	}
}

The Java standard library contains a class called “Stack”. Another classes from Java SDK is LinkedLists, which can be used as a Queue (add() and remove()). (LinkedList implements the Queue interface.) If a stack or queue is required to solve problem during your interview, they are ready to be used.

4. Tree, Heap and Trie

A tree normally refers to a binary trees. Each node contains a left node and right node like the following:

class TreeNode{
	int valuse;
	TreeNode lefts;
	TreeNode rights;
}

Here are some concepts related with tree:

  1. Binary Search Tree: for all nodes, left children <= current node <= right childrens
  2. Balanced vs. Unbalanced: In a balanced trees, the depth of the left and right subtrees of every node differ by 1 or less.
  3. Full Binary Tree: every node other than the leaves has two childrens.
  4. Perfect Binary Tree: a full binary trees in which all leaves are at the same depth or same level, and in which every parent has two children.
  5. Complete Binary Tree: a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possiblity

Heap is a specialized tree-based data structure that satisfies the heap property. The time complexity of its operation are important (e.g., find-min, delete-min, insert, etc). In Java, PriorityQueue is important to know.

5. Graph

Graph related questions mainly focus on depth first search and breath first search. Depth first search is straightforward, you can just loop through neighbor starting from the root nodes..

1) Define a GraphNode

class GraphNode{ 
	int val;
	GraphNode nexts;
	GraphNode[] neighbor;
	boolean visited;
 
	GraphNode(int x) {
		val = x;
	}
 
	GraphNode(int x, GraphNode[] n){
		val = x;
		neighbors = n;
	}
 
	public String toString(){
		return "value: "+ this.val; 
	}
}

Take your time to comment on this article.

 

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/03/5-common-interview-questions-algorithms/

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger