Javascript is most popular scripting language and it is always in first row of stackoverflow tags page.
So why we should not use javascript to implement algorithms. Generally in coding interview, you will have freedom to write algorithms in your favorite language. If you are good at Javascript, then why to go with other languages.

Lets implement some algorithm stuff with JS. The below topics were covered in this article

1) String/Array, 2) Linked List, 3) Tree, 4) Heap, 5) Graph, 6) Sorting, 7) Recursion vs. Iteration,
8) Dynamic Programming, 9) Bit Manipulation, 10) Probability, 11) Combinations and Permutations

Debugging JS

to print content in console, we have to use below statement
console.log('hello world'); 
Use chrome or firefox to debug Javascript. Just "right click" > "inspect element" > "click on console".
Then you will see console.

Some useful operations in Javascript

To get char array from string

"Hello world!".split('')
["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", "!"]

Get char from index

var str = "HELLO WORLD";
var res = str.charAt(0); // 'H'

Get length of string

var str = "HELLO WORLD";
var res = str.length; // 11

Get length of array

var sarr = ["H","E"];
var res = sarr.length; // 2

Substring

var str = "HELLO WORLD";
var res = str.substring(1); // "ELLO WORLD"
var res = str.substring(1,4); //"ELL"

Sort array

var nums= [4,2,3,1];
nums.sort(); // 1,2,3,4

Convert string to int

var num = parseInt("10");

Convert array to string

var sarr = ["H","E"];
var res = sarr.join(''); // HE

Slice and copy array

var nums= [4,2,3,1];
var newArr = nums.slice(); // copy array
var newArr = nums.slice(1,3); // 2, 3

Reverse the array

var nums = [1, 2, 3, 4];
nums.reverse(); // [3, 4, 2, 1]

Remove Item From Array At Specific Index

var num = [1,2,3,4]; // array
var j = 2; // index value
num.splice(j,1); // removing 3 from the array

Add Item To Array At Specific Index

var num = [1,2,3,4]; // array
var j = 2; // index value
num.splice(j,0,5); // Add 5 at index 2

Verify if variable is not a number

var str = "sample";
isNaN(str) // true
var num = 9;
isNaN(num) // false

Iterate characters

for (var c = 'a'; c <= 'z'; c = nextChar(c)) {
    ........
}

function nextChar(c) {
    return String.fromCharCode(c.charCodeAt(0) + 1);
}

Map (key - value pair) implementation

// put value in map
var map = {};
map['mykey'] = 'myvalue';

// check whether map contains key
if(map['mykey'] != null) {
   // map contains key
}

// get value to the key
var value = map['mykey'];

// a method for getting key of a target value
function getKey(map, target){
    for (var i in map)     {
        if (map[i] == target) {
            return i;
        }
    }
    return null;
}

// delete the key-value pair
delete map[key];

Stack Implementation

var stack = [];
//push value into stack
stack.push(1);
//pop value from stack
stack.pop(1);

Queue Implementation

var queue = [];
//push value into queue
queue.push(2);         // queue is now [2]
queue.push(5);         // queue is now [2, 5]
//pop value from queue
var i = queue.shift(); // queue is now [5]
alert(i);            

Classic Problems (String / Array)

  1. Rotate Array
  2. Reverse Polish Notation
  3. Isomorphic Strings
  4. Word ladder with Javascript
  5. Median of two sorted array
  6. Merge Intervals
  7. Insert Intervals
  8. Two Sum
  9. Merge sorted arrays
  10. Valid Parenthesis
  11. Three Sum
  12. Three Sum Closest
  13. Four Sum
  14. Longest Valid Parenthesis
  15. Minimum Size Sub Array Sum
  16. Longest Consecutive Elements Sequence
  17. Add binary values
  18. Largest Rectangle In Histogram
  19. Longest Sub String Without Repeating Characters
  20. Longest Sub String With Specified Number Of Unique Characters
  21. Longest Common Prefix
  22. Minimum Window Sub String
  23. Minimum Path Sum Of Triangle
  24. Anagrams
  25. Arrange Non Negative Integer Array To Generate Largest Number
  26. Container With Most Water
  27. Count and say
  28. Gas Station Problem
  29. Find Kth Largest Element In Array
  30. Letter Combinations Of Phone Number
  31. Max Points
  32. Pascal Triangle
  33. Pascal Triangle II
  34. Repeated DNA Sequence
  35. Search Range
  36. Simplify Path

Matrix


------still writing-------

0 comments:

Blogroll

Follow this blog by Email

Popular Posts