Suvankar Satpati

Fan of well made things regardless of the technology behind them. Creative problem solver. Web enthusiast.

Read this first

Stack and Queue

Stack
is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out).

Mainly the following three basic operations are performed in the stack:

Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.

Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.

Peek: Get the topmost item.

How to understand a stack practically?

There are many real life examples of stack. Consider the simple example of plates stacked over one another in canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it...

Continue reading →


DS and Algo

Given a list of area telephone codes along with their location, find the location of a given telephone number. The area codes can be minimum three digits and maximum six digits. Area codes are unique for a particular length between three and six, inclusive. For eg: if 080 is an area code, 0804 cannot be another code , as for length 3, 080 occurs in both codes. You are allowed to process the list however suitable to enhance the efficiency.

Find the second maximum element in an unsorted array of integers, using single for loop

Solution One:

int min = 0;
int secondmin = 0;

if(array[0] < array[1]) {
  min = array[0];
  second_min = array[1];
} else {
  min = array[1];
  second_min = array[0];
}
for(i=3; i<array.length; ++i) {
  if(array[i] < min) {
    secondmin = min;
    min = array[i];
  } else if (array[i] < secondmin) {
    secondmin = array[i];
  }
}

Sort the array using Merge...

Continue reading →


Linked List

Linked List

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers.

Why Linked List?

Arrays can be used to store linear data of similar types, but arrays have following limitations.

1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.

2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.

For example, in a system if we maintain a sorted list of IDs in an array id[].

id[] = [1000, 1010, 1050, 2000, 2040].

And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements...

Continue reading →


Sorting Algorithms

The sorting problem is as following:
Take a bunch of numbers, put them in order.

Input: We have a sequence <a1, a2, a3, ......, an>of number

.

Output: A permutation (rearrangement) <a1', a2', a3', ....., an'> of those numbers.

such that: a1' <= a2' <= ... <= an'

Psudo code of Insertion-Sort :

Insertion-Sort(A,n) //sort A[1..n]
    for j <-- 2 to n
        do key <-- A[j]
            i <-- j - 1
            while i  > 0 and A[i] > key
                do A[i + 1] <-- A[i] 
                    i  <-- i - 1
                A[i+1] <-- key

View →


Tree

Tree: Tree is hierarchical data structure, where array, linked list, stack and queue are linear data structure.

why should we use tree data structure

  • To store information which naturally forms hierarchy, i.e. file system on a computer
  • Tree provides moderate search and insertion speed, better then linked list slower then array
  • Tree does’t have upper limit like linked list

Tree vocabulary

Root: top most node

Children: under some element or root

Parent: element directly under some element

Leaves: element which has no children

BST

The following is definition of Binary Search Tree(BST) according to WikiPedia

Binary Search Tree, is a node-based binary tree data structure which has the following properties:

The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
...

Continue reading →


Devise and Mongoid in Rails 4

To use MongoDB as a database in rails application we need to use mongoid.

App source Code

mongoid


Mongoid is an Object-Document-Mapper (ODM) for MongoDB written in Ruby
In a Rails application mongoid provide functionality like ActiveRecord, but not exactly same, because MongoDB is a document-orinted database, and that mappers is called Object Document Mappers (ODM) which maps between an Object Model and a Document Database

To use mongoDB, most of the configuration comes down by making sure that we are am not loading ActiveRecord. One way to avoid loading ActiveRecord at time of creating rails project using following switch

—skip-active-record

The Rails command that generate application skeleton now has an option -O , which commands rails to skip active record.

So, the final app skeleton will look like so:

rails new appName -T -O

open your Gemfile and add:

source
...

Continue reading →


Capybara Cheat Sheet

Capybara helps to test web applications by simulating a real user.

Navigating

visit products_path
visit '/products/new'
visit post_comments_path(post)

Clicking links and buttons

click_button 'New Product'
click_link 'Delete'
find("product_{@product.id}").click_link 'Delete'  if there exists multiple link with the same name
click_link('id-of-link')
click('Link Text')  Click either a link or a button

Interacting with forms

fill_in :Name, :with =>  'Nexus 5'
fill_in 'Price', :with => '25000'


Querying

page.should have_content '25000'
page.should have_no_content '30000'
find_field('Name').value.should eq 'Nexus 5'
find_field('Price').value.should eq '30000'
page.has_xpath?('//table/tr')
page.has_css?('table tr.foo')
page.has_content?('foo')
page.should have_xpath('//table/tr')
page.should have_css('table tr.foo')
find_link('Hello').visible?
find_button('Send').click
...

Continue reading →


Creating a Rails 4 app from scratch following TDD, using Rspec

I am going ahead to create a simple Product Catalog type rails project. I will follow test driven development methodology to build up this application. By default Rails use test unit as testing framework, but like most of rails developer I prefer Rspec.

I am going to use mongoDB with this project (I love mongo, that’s why !). To use mongoDB, most of the configuration comes down by making sure that I am not loading ActiveRecord. One way to avoid loading ActiveRecord at time of creating rails project using following switch

—skip-active-record

The Rails command that generate application skeleton now has an option -O , which commands rails to skip active record.

So, the final app skeleton will look like so:

rails new product-catalog -T -O

Now open the Gemfile and specify required gem for this project. I am going to use rspec-rails for this project to write test. To automate the...

Continue reading →