Postgres – change column from type text to uuid

I created a table where I was entering UUID’s as text. But then realized postgres supports uuid column types.

But a simple alter table alter column didn’t work and got me the following error:

$ ALTER TABLE packages USING package_id::uuid;
ERROR:  syntax error at or near "USING"
LINE 1: ALTER TABLE packages USING package_id::uuid;

Here’s the solution to change the column type from text to uuid.

ALTER TABLE packages ALTER COLUMN package_id TYPE uuid USING package_id::uuid;

GitHub – Creating a Pull Request without Fork

Ran into a situation where Forking was disabled on the Git instance and had to create a pull request. Here’s the steps that I followed to create a new Pull Request, Commit changes that were already made on the local dev branch, merge the pull request to dev and finally remove the PR Branch.

1. Login to GitHub and navigate to the repository
2. Switch to the dev branch
3. Create a new branch off the dev branch and let’s call it JIRA-123
4. On your local machine, let’s say you are on dev branch and you have the changes ready – an existing file modified and a new file created. To view the changes, type git status -bs

## dev...origin/dev
?? src/main/mule/truststore-salesforce-connector-api.p12

5. Since you’d like to commit the changes onto the new branch JIRA-123, execute git pull to get a copy of the branch locally.
6. Execute git branch -a to get a list of all branches

* dev
  remotes/origin/HEAD -> origin/master

7. If for any reason GIT doesn’t allow you to switch branches and asks you to commit changes first, you may have to stash the changes. To stash the changes, execute the command git stash that will save the changes in a queue with your latest changes being in top.
8. Now you can check out to the branch by executing git checkout JIRA-123
9. Apply the stash with the command git stash apply. This would apply the latest stashed code but will still preserve the changes on the stash. If you’d like to apply the stash locally and drop it at the same time, you could use git stash pop.
10. Add the new file to the commit git add src/main/mule/truststore-salesforce-connector-api.p12. Commit the changes and push the branch git commit -a -m "Message for the commit"
11. Finally push the changes to the remote repository using git push. Now your changes have been pushed to GitHub and you’re ready to create a Pull Request
12. Getting back to GitHub, stay on branch JIRA-123 and click on the button New Pull Request
13. Select your base: dev and compare: JIRA-123. Add comments and click on Create pull request.
14. Request is created and ready for review. As a reviewer, review changes and click on Merge pull request and Confirm merge.
15. GitHub gives you the option to delete the branch JIRA-123 which you should go ahead and delete as it is no longer needed. So click on Delete branch.
16. After merging a pull request and deleting the branch from GIT, you’ll noticed that the local git repository still shows the remote and local branches. When branches get deleted on origin, your local repository won’t take notice of that. You’ll still have your locally cached versions of those branches (which is actually good) but git branch -a will still list them as remote branches.
17. You can clean up the remote branches locally using git fetch --prune.
18. This would still leave your local branch intact that you probably won’t need. Delete the branch locally like this git branch -d JIRA-123

Sending emails from Mulesoft

You could use the SMTP connector to send emails from Mulesoft. I am sure you can find plenty of examples online to send emails via Gmail. That was easy to do with the following configuration:

<email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="4af35a74-e02e-41be-9125-7fb2da34317c" >
    <email:smtp-connection host="" user="" password="your-password" port="587">
        <email:properties >
            <email:property key="mail.smtp.starttls.enable" value="true" />

But it got a little challenging to send emails through secure SMTPS protocol. Here’s the configuration that you could use to setup the email. In a nutshell, the SMTPS requires you to provide truststore configuration but according to the documentation, if the tls:context is empty (no tls:key-store or tls:trust-store defined), then the default values of the JVM are used, which usually include a truststore with certificates for all the major certifying authorities.

<email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="0352f798-fd91-4307-bf7d-8f3a092dddd5">
    <email:smtps-connection host="" user="your-username" password="your-password">
        <tls:context enabledProtocols="TLSv1.2">
            <tls:trust-store />

Priority Queue/Heap in Python

Priority Queues are an efficient way to get the min or max element from a list with O(1) time v/s using a min() function that loops through the entire list and gets the min or max element in O(n) time. The cons of using this approach is that every time an entry is added or removed from the heap, the elements are re-arranged so that the heap property of storing the min/max element at the root of the data structure is satisfied.

Python comes with an in-built module named “heapq” to implement priority queues.


  • heapq implements min heap, i.e. the smallest element is stored at the root node of the data structure
  • max-heap isn’t supported by the heapq module by default. The workaround is to multiply the input numbers by -1 and add them to the heapq so that the lowest element now is the biggest element from the source list. While returning the largest element, multiply the popped element by -1 to get the original value back.
  • The elements are modified in-place. To perform heapify operation on a list, pass the list to the heapify() method which would convert it to a min-heap in place.
  • Every time an element is added or removed to/from the heap, the heap is re-ordered to maintain the priority queue
  • Re-arranging the elements in a priority heap takes O(log n) time v/s removing an element from a list takes O(n) and re-arranging them takes O(n log n)

Operations on a heap

  • heapify(iterable): This function is used to convert the iterable into a heap data structure. i.e. in heap order.
  • heappush(heap, element): This function is used to insert the element mentioned in its arguments into heap. The order is adjusted, so as heap structure is maintained.
  • heappop(heap): This function is used to remove and return the smallest element from heap. The order is adjusted, so as heap structure is maintained.
  • heappushpop(heap, element): This function combines the functioning of both push and pop operations in one statement, increasing efficiency. Heap order is maintained after this operation.
  • heapreplace(heap, element): This function also inserts and pops element in one statement, but it is different from above function. In this, element is first popped, then the element is pushed.i.e, the value larger than the pushed value can be returned. heapreplace() returns the smallest value originally in heap regardless of the pushed element as opposed to heappushpop().
  • nlargest(k, iterable, key = fun): This function is used to return the k largest elements from the iterable specified and satisfying the key if mentioned.
  • nsmallest(k, iterable, key = fun): This function is used to return the k smallest elements from the iterable specified and satisfying the key if mentioned.

Useful tricks for solving coding problems

Sharing some useful tricks for solving coding problems.

1. Get count of adjacent characters in a string

Use Cases

  1. Helpful for solving the Count and Say problem. e.g. given the string ‘aaabbcccccddddefggghh’ the expected output should be ‘3a2b5c4d1e1f3g2h’.
  2. Counting the number of non-space segments in a string.
  3. String compression
  4. Attendance check
  5. Long pressed name


  1. Have 2 pointers start and end that serve as a window. Initialize them to begin of string. The idea is to keep moving end until it reaches a different character than start OR it reaches end of word.
  2. Run an outer while loop that ensures that end doesn’t go beyond the length of string
  3. Run an inner while loop to check if end of word or same character than start is encountered. Until the end is same as start, keep incrementing end. Most importantly: Check for end of string before char matching so that the following condition (char match) isn’t executed if end of string is reached.
  4. After the inner while loop ends, end is either end of word or different than start. Compute the length of the char occurrences. Append the length and the last char to the output and move start to end.
  5. Execution will return to the outer loop, will ensure end of string is not reached and begin execution with the new start position.
def countAndSayString(string):
  start, end = 0, 0
  result = ''
  # Outer while loop to ensure end is less than string length
  while end < len(string):
    # Inner while loop to move end until it equals start. 
    # In addition also ensure that end doesn't go beyond length
    # Always put the len check before so following condition isn't executed if end is over limit
    while end < len(string) and string[start] == string[end]:
      end += 1
    len_chars = end-start
    result += str(len_chars) + string[start]
    start = end
  return result

string = 'aaabbcccccddddefggghh'
result = countAndSay(string)
print(f'Result is: {result} | {result=="3a2b5c4d1e1f3g2h"}')

2. Adding very large numbers represented as string

Given the restriction, you cannot convert the strings to integers because the language won’t support it.


  1. Left pad the shorter string by 0
  2. Initiate carry with 0 and keep adding numbers from the LSB with carry. The remainder will be left appended to the result and the dividend will be passed to carry
  3. If carry remains, convert to string and left append to result

3. Palindrome by removing at most one character

Check to see if string can be made palindrome by removing at most one character.


  1. Start from left and right and keep moving pointers inwards
  2. If chars don’t match, then taking either one of them out should result in a palindrome string if so
  3. Compare the remaining string without left char and string without the right char. If either one is palindrome, then we can remove the other char that would result in string being palindrome

4. Repeated string match

Given two strings A and B where A is shorter than B, find out how many times you will have to repeat A such that B becomes a sub-string of A given this is possible., e.g. A = “abcd” and B = “cdabcdab”, A must be repeated 3 times so B becomes a sub-string of A.


  1. The length of A must be at least B’s length so B can be a sub-string of A
  2. Give an additional buffer to repeat A (draw on paper paper to find out why)
  3. Divide len(B) by len(A) to get the min repeatitions and add another one and loop through to check if B is sub-string and return the min possible number

5. Useful to check on upper and lower case string problems

You can convert characters to ASCII and vice-versa in python by using the ord() and chr() methods. It’s useful to check on upper/lower case characters and to convert them without using in built binaries.

  1. ord(c) converts the character c to it’s ASCII value. Upper case starts from 65 and lower case starts from 97
  2. chr(n) converts the ASCII value to it’s character equivalent.

Node.js equivalent of python’s if __name__ == ‘__main__’

As I was building some modules for the automated testing framework, I realized that it would be nice to run the functions in the modules themselves to test individually but not have them executed when imported by another file. That’s when I loved missed Python’s if __name__ == ‘__main__’ but quickly found out that the same can be achieved in node.js as well.

When a file is run directly from Node.js, require.main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module. For a file foo.js, this will be true if run via node foo.js, but false if run by require(‘./foo’).

var fnName = function() {
    // main code

if (require.main === module) {

Installing Node.js on Mac

Had a hard time when I shouldn’t have. I installed it from the installer utility on Found some issues and then installed using brew probably as root which screwed up a few things. But just listing down what users should be going.

1. Run the installer as yourself – not root as it would mess up the directory ownership on /usr/local and if you did that already you could fix that by using this command

sudo chown -R $(whoami) $(brew --prefix)/*

2. Check the version that you are about to install, to check if you’re getting the stable release or the latest release:

brew info nod

3. Install node using brew

brew install node

Counting Sort

Can sorting be done in time complexity of O(n). The answer is Yes. But, the shortcoming is that the toll it takes on the space. As you will go through the algorithm and notice, if the input array contains numbers whose range is way more than the size of the array, the technique can be very hard on space. But anyways, here’s the algorithm if the size of the numbers and the range of the numbers isn’t very large.

1. Create an array of size max(input)+1 to account for the element 0. This array will store the frequency of input numbers at it’s index, e.g. if element 2 occurs 3 times in the input, then count[2] = 3
2. Iterate through the count array and create a cumulative sum that will determine the position in the input numbers in the output array
3. Now iterate through the input array, get it’s position from the count array and store the number at position-1 in the output array. Decrement the count by 1 so the next occurrence of the number is stored at the corresponding prior position


def countSort(nums):
  max_n = max(nums)
  len_n = len(nums)
  count = [0] * (max_n+1) # Account an extra space for element 0
  output = [0] * len_n
  # Set the frequency of input numbers in the count array
  for n in nums:
    count[n] += 1

  # Create cumulative sum in the count array to get the position of the next number in the output array
  for i in range(1, max_n+1):
    count[i] += count[i-1]

  # Iterate through input, get the pos for output and store in output; decrement the count so next number is stored at a different position
  for n in nums:
    pos = count[n]
    output[pos-1] = n
    count[n] -= 1
  return output

nums = [1,2,5,2,3,1]
result = countSort(nums)
print(f'Counted Sort on {nums} is {result} | {result== [1, 1, 2, 2, 3, 5]}')

nums = [4,8,10,2,5,1,9,2,7,1]
result = countSort(nums)
print(f'Counted Sort on {nums} is {result} | {result==[1, 1, 2, 2, 4, 5, 7, 8, 9, 10]}')

Cloning from a private GitHub repository

I tried many changes (config file, etc) but eventually this is what worked for me:

1. Create a key-pair that will create files on your local ~/.ssh directory. Upload the public key on github

ssh-keygen -t rsa -b 4096 -C ""

2. Add the key to your ssh-agent

ssh-add -K ~/.ssh/id_rsa_gh

3. Use the ssh clone URL to clone the repository:

git clone

MuleSoft – Connect to PostgreSQL Database

MuleSoft by default supports connectors to MySQL, SQL Server and Oracle. But it offers to connect to any generic database as long as we can provide the JDBC connector to the project. Being a JAVA application, the connector needs information for the Driver class name, URL which can get very specific. So here’s how you can connect to the PostgreSQL server from Mule.

1. Drag a database widget on to the flow

2. Add a new Connector configuration

3. Select Connection type as “Generic Connection”

4. Click on Configure to add the JDBC driver and select Add Maven dependency (pre-requisite: Download the latest JDBC driver from the website:

5. Click on Install

6. Click on Browse and select the JDBC driver that was downloaded. This will be imported into the project, so doesn’t matter where the downloaded file lives. Click Install. You’ll get a notification that the driver was successfully imported.

7. Click on Finish

8. Back on the config screen, set the following values on the screen:
URL: jdbc:postgresql://localhost:5432/your_database_name
Driver class name: org.postgresql.Driver
User: your_database_user
Password: your_database_password

9. Click on Test connection.. and if you’re successful, you should be all set.