Page 1 of 2

### Merge two sorted lists

##### Jul 1

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

For this we create a dummy head before the new list so that we don’t have to deal with cases for initializing head of new list. Then new list’s head could be returned as dummy head’s next node.

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26``` ```# Time complexity: O(n) def merge_two_lists(l1, l2) dummyhead = Node.new(0) p = dummyhead while(!l1.nil? && !l2.nil?) if (l1.val < l2.val) p.next = l1 l1 = l1.next else p.next = l2 l2 = l2.next end p = p.next end if (!l1.nil?) p.next = l1 end if (!l2.nil?) p.next = l2 end dummyhead.next end ```

### Two sum

##### May 15

In order to prepare myself in a better way, I’ve started doing problems on Leetcode. Here’s starting with first one -

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

For e.g.

 ```1 2 3 4``` ```Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. ```

Like every other problem, this could be solved by multiple approaches. My approach is using one hash table. This way when we iterate and insert elements into the table, we also look back to check if current element’s complement already exists in the table. If it exists, we have found a solution and return immediately.

 ```1 2 3 4 5 6 7 8 9 10 11 12``` ```# Time complexity: O(n) # Space complexity: O(n) def two_sum(nums, target) nums_hash = {} nums.each_with_index do |i, ix| if (nums_hash.has_value? (target - i)) return [nums_hash.key(target - i), ix] end nums_hash[ix] = i end end ```

### Matrix Spiral Print

##### May 11

Given a 2D array (matrix), print all elements in a clockwise (spiral) manner

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39``` ```# Time complexity: O(n*m) # Space complexity: O(1) def matrix_spiral matx topRow = 0 btmRow = m-1 leftCol = 0 rightCol = n-1 while (topRow <= btmRow && leftCol <= rightCol): # print the next top row (leftCol..rightCol).each do |i| print matx[topRow][i] end topRow++ # print the next right hand side column (topRow..btmRow).each do |i| print matx[i][rightCol] end rightCol-- # print the next bottom row if (topRow <= btmRow) (rightCol..leftCol).each do |i| print matx[btmRow][i] end btmRow-- end # print the next left hand side column if (leftCol <= rightCol) (btmRow..topRow).each do |i| print matx[i][leftCol] end leftCol++ end end end ```

### Delete a node from Linked List

##### May 11

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

We can traverse the list from the beginning until we encounter the node to delete. But in this situation, we don’t know where the head of the list is, we only have a reference to the node to delete.

The usual way of deleting a node node from a linked list is to modify the next pointer of the node before it, to point to the node after it.

So we take the `@value` and `@next` from the input node’s next node and copy them into the input node. Now the input node’s previous node effectively skips the input node’s old value!

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17``` ```# Time complexity: O(1) # Space complexity: O(1) def delete_node(node_to_delete) # Getting next node which we want to move to next_node = node_to_delete.next if next_node # Replace value and pointer with next_node's value # and pointer node_to_delete.value = next_node.value node_to_delete.next = next_node.next else raise "Can't delete the last node." end end ```

### Reverse a Linked List

##### May 3

Algorithm and Data structures have been one of my weak point for quite some time now. And after working for more than 6 years a lot of these are forgotten. Past few days have been brushing up my skills and will be documenting here.

Starting with Linked list, here’s how to reverse a linked list.

Assume that we have linked list 1 → 2 → 3, we would like to change it to 1 ← 2 ← 3.

A simpler way to come up with solution is by iterative way. We can traversing the list, change the current node’s next pointer to point to its previous element. Since a node does not have reference to its previous node, you must store its previous element beforehand. You also need another pointer to store the next node before changing the reference. Do not forget to return the new head reference at the end!

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25``` ```# Time complexity: O(n) # Space complexity: O(1) def reverse(head_of_list) current = head_of_list previous = nil next_node = nil # until we have 'fallen off' the end of the list while current # copy a pointer to the next element # before we overwrite current.next next_node = current.next # reverse the 'next' pointer current.next = previous # step forward in the list previous = current current = next_node end return previous end ```

### Convert binary data to image in ColdFusion

##### Jun 20

Converting binary data to any image format has been made pretty easy in ColdFusion

 `1` ``` ```

As above, binary data first has to be converted to base 64 using method `tobase64`, which can be further converted to required format using ColdFusion supported method `ImageReadBase64`.

### How to resolve fatal: remote origin already exists

##### Jun 17

The error message indicates, there is already a remote configured with the same name. So you can either add the new remote with a different name or update the existing one if you don’t need it.

First of all Github has a good documentation on renaming a remote to help with this issue.

Check your remote repositories that you’re connected to

 `1` ```git remote -v ```

This should return a list in following format

 ```1 2``` ```origin git@github.com:github/git-reference.git (fetch) origin git@github.com:github/git-reference.git (push) ```

That might help you figure out what the original ‘origin’ pointed to.

At this moment you could follow either of the below methods to resolve this error based on your requirements.

• Remove the origin and your new repository as in

git remote rm origin git remote add origin newrepo-url

• If you want to keep the remote connection that you see with the -v, but still want to follow the workflow without having to remember 'github’ (or some other name) for your repo, you can rename your other repository with the command:

git remote rename [current name] [new name]

as in:

 `1` ```git remote rename origin oldreponame ```

Once done, you should be able to change origin to new repository with

 `1` ```git remote add origin newrepo-url ```
• You could also use a new name for remote url as

git remote add github newrepo-url

Remember though, everywhere now onwards you’d be using `github` as your remote name. For example `git push origin master` should now be `git push github master`.

### Get image info in ColdFusion

##### Jun 9

The following example shows how you can get an image’s information (width, height, alpha, etc) using the `<CFIMAGE>` tag in ColdFusion,

 ```1 2``` ``` ```

or by using the `imageInfo()` as

 `1` ``` ```

Both of which should you image information in a nice `cfdump` format.

### How to kill process and free port

##### May 31

While setting up this blog for once I got into error where it was erroring when I tried to start the server using `middleman server` command.

The error was -

 `1` ```Port 4567 is unavailable. Either close the instance of Middleman already running on 4567 or start this Middleman on a new port with: --port=4568 ```

This error meant I messed up in one of my earlier session and may have closed my terminal while server was running.

This is easy to resolve, by first finding the PID of the process.

 `1` ```\$ lsof -wni tcp:4567 ```

Then use the number in the PID column to kill the process (in my case it was 44995)

 `1` ```\$ kill -9 44995 ```

Hope this helps anyone having issues freeing port.

### Learning Ruby

##### May 18

Finally I’ve taken my first paid plunge into learning Ruby language. For the next 7 weeks I’ll be totally immersed in coding Ruby under the guidance of Satish Talim.

Even thought I’m using Ruby professionally, I’m far away from even being called an intermediate developer in it. It all started as helping out testers once in a while and soon I fell in love with the language. In order to be more able, I’ve been through Codecademy Ruby track which gave me nice jumpstart to what I was doing at work. I languished at various other sites and tutorials before I decided it’s better to take some professional help and shell out some moolah.

I had been reading Ruby Learning blog for some time now and was waiting for their next batch to start. I was still in dilemma between above and Pragmatic Studio. Eventually, it all boiled down to cost of course and since I could afford this without reiterating my monthly budget, it was a clear winner.

Along with this, I’m planning to do some code katas at Code Wars and also learn some more stuff at Ruby Monk.

I see myself as a better Ruby Developer and a step closer to get that Rails Developer job after this course. Lets hope for the best.

Next page