Monday, May 27, 2013

Stack and Queue Algorithm Questions


  1. Use single array to implement 3 stack.
  2. Implement stack class. Write push , pop and peek function. Write a function MIN which return minimum value in O(1).
  3. Implement queue using 2 stacks.


Sorting and Searching Algorithm Questions


  1. Given two sorted array A and B. A is large enough to hold B. Merge the arrays in sorted order.
  2. Write a function which sort the array of string such that 2 anagram are together.
  3. A array is sorted in increasing order and rotated many times. Find a number N in given array.
  4. You have 10 GB of file with each line having 1 string.Sort the file.
  5. Given sorted array of string. The empty string is inserted between each string. Find a given string in array.
  6. Given a matrix MxN which is sorted by row and column in increasing order. Find given element .

Tree and Graph Algorithm Questions


  1. Implement a function to check if binary tree is balanced. A balanced tree is whose left and right sub-tree height do not differ more than 1.
  2. Given a directed graph. Write algo to find route between 2 nodes.
  3. Given a sorted increasing order arrray. Create binary search tree with minimum height.
  4. Given a binary tree. Create a link list of all nodes at each depth.
  5. Write function which checks whether given binary tree is binary search tree.
  6. Write algo for next node "in order successor" of a given node in binary search tree.
  7. Write function of find common ancestor of 2 nodes. This may not be BST and avoid using other data structure.
  8. Given 2 very large binay tree. Find one tree is sub-tree of another at node N. So that if we cut tree at node N then both the tree are identical.
  9. Given the binary tree. Print all the paths whose sum is equal to gicen value. Path can start and end anywhere.

Algorithms learning resorces

MIT lectures
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/

Tuesday, April 2, 2013

Link List Algorithm Questions


  1. write a code to remove duplicates from unsorted link list. How would you solve it if temp buffer is not given.
  2. write algo to find nth to last element of singly link list.
  3. implement the algorithm to delete a node in the middle of a singly link list given only access to that node.
  4. write a algo to partition a linked list around a value such that all nodes less than x come before it and all nodes with value greater than x comes after it.
  5. suppose number are represented by singly linked list in reverse order. Eg . 123 is represented as       3->2->1. Write a function which could add these numbers and represent in link list.
  6. suppose number are represented by singly linked list in same order. Eg . 123 is represented as          1->2->3. Write a function which could add these numbers and represent in link list.
  7. Detect the loop in the link list. Also detect the loop node. Eg . a->b->c->d->e->b.   E is pointing to b so there is loop in link list.
  8. Implement a method to check if link list is palindrome.

Array Algorithm Question


  1.  Given an image represented by NxN matrix. Each pixel is represented by 4 bytes. Write a method to rotate image by 90 degree. Can you do in place ?
  2. Write an algo so that if an element in MxN matrix is 0 then entire row and column are set to 0.
  3. Given the array of integer. Find contiguous sequence of array with largest sum. Return sum and also calculate the start and end index of sub-array.
  4. Design algo to find pair of integer in a given array with specified sum.


String Algorithm Questions


  1.  Implement the algo to determine if a string has all unique characters. What is you cannot use additional data structure ?
  2. Implement the string reverse function.
  3. Given two string determine one is permutation of another.
  4. Implement the method to perform basic string compression techniques by using the count of repeated character. For example "aabbbccdddde" can be represented as "a2b3c2d4e1". If compressed string is not smaller than original string then return the original string.
  5. Write a code to check if string s1 is rotation of string s2.(eg. "hello" is rotation of "llohe" ). you have given method "substring" which check one word is substring of another. Use it only once to determine it.
  6. Calculate the occurrence of given word in a book.


Monday, March 11, 2013

Javascript Tutorial

http://www.scribd.com/doc/48750547/AJS

Nice presentation touches main concept of the language.


Saturday, March 9, 2013

Function Literal vs Function definition


Function Literal

var Class = function () {};
vs

Function Definition
function Class () {};


the former is "hoisted" to the top of the current scope before execution. For the latter, the variable declaration is hoisted, but not the assignment. For example:
// Error, fn is called before the function is assigned!
fn();
var fn = function () { alert("test!"); } 

// Works as expected: the fn2 declaration is hoisted above the call
fn2();
function fn2() { alert("test!"); }


http://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor

Adding methods to a class vs adding methods to a class's prototype

https://www.quora.com/JavaScript/What-are-advantages-to-adding-methods-to-a-class-vs-adding-methods-to-a-classs-prototype

Snippet 1 (Adding methods to a class)

var myClass = function(prop1, prop2) {
    this.prop1 = prop1;
    this.prop2 = prop2;
    
    this.method1 = function() {//blah}
    this.method2 = function() {//blahblah}
}

Snippet 2 (Adding methods to a class's prototype)


var myClass = function(prop1, prop2) {
    this.prop1 = prop1;
    this.prop2 = prop2;
}
myClass.prototype.method1 = function() {//blah}
myClass.prototype.method2 = function() {//blahblah}


Snippet 1

  • Two functions are created for every construction of 
    myClass
  • can give you access to private variables and constructor arguments

Snippet 2


  • the two functions are created once: when the prototype is filled.
  • gives better memory usage
  • Methods that inherit via the prototype chain can be changed universally for all instances
  • For instance, you can extend the built-in String object by adding a trim function to it and all future instance of that object will share the trim function.

http://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor