Expiring Local Storage Objects in JavaScript

All the modern browsers have multiple types of storage mechanisms for using in your web applications. You may have already heard of cookies which are small bits of information you can store and they will be automatically expired.

However, cookies can only store small amounts of information. The other kind of storage is sessionStorage, where you can store big chunks of information. However all the data stored in this will be lost as soon as you close the browser tab.

Browser Storages

Local storage provides an intermediary option, it can store large amount of information and it will not be lost after the user closes the tab or browser. The data is persisted across sessions. However, we may want a better solution. We want to store large chunks of data across sessions, but we still want to have an option of invalidating after a certain period of time.

Read More

Creating Alexa Skill using Java and AWS Lambda

alexa

The Goal

Gone are the days where we build applications and just think about graphical user interfaces, look and feel etc., There is a new interface that is gaining popularity. As Amazon, Google and Apple are bringing in voice assistants, it has become extremely important for us to learn how to build voice-activated applications. Voice based commands are much more complex than a GUI, user actions on our application are limited to button clicks, combo box selections, typing in text fields comprise of the majority of instructions. With touchscreen, we can see some more actions users can perform like swipe, pinch, zoom, rotate etc., However, with voice a single user may ask our application to do specific tasks in wide variety of ways, to increase the complexity even further different people may use and each have their own way of speaking. Alexa provides a simple framework to build these skills. In this article I will show you how you can build your own Alexa Skills.

Read More

Top View of Binary Tree from left to right without Recursion

44231372726515065

Top View of Binary Tree

The Problem

Imagine you have a binary tree and wants to get all the nodes that will be visible when seen from the top of the tree. How do you print all such nodes? Final output for this tree should be 7, 13, 23, 44, 51, 65. A similar problem about printing right view is given in the previous post about Right View of Binary Tree without Recursion

Read More

A Great Tool for Datastructure Algorithm Visualisation

There have been many times, we get doubts about how a certain algorithms work. There is no better way than being able to visualise that algorithm. For example check this page where you can see how each sort works on same array input. I used to embed their webpage but now they have blocked cross site embedding, hence you need to go their page to see the animation.

https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

Open the URL and click on the algorithm so that you can visualise how each algorithm sorts.

These are created by David Galles, an Associate Professor of department of Computer Science University of San Francisco.

You will be able to see lot more visualizations by going to this page.

https://www.cs.usfca.edu/~galles/visualization/Algorithms.html


Right View of Binary Tree without Recursion

44231372726515065

Right View of Binary Tree

The Problem

Imagine you have a binary tree and wants to get all the nodes that will be visible when seen from the right side of the tree. How do you print all such nodes? Final output for this tree should be 44, 51, 65, 26. In other words, the first nodes we touch upon if we draw horizontal lines from right side of the tree. Read on to find the solution.

Read More

Collapsible panel using OJet & jQuery

Before we begin, let me show you the final output we are going to get. Click Run Pen button if you want to see the collapsible panel in action

See the Pen Collapsible and Expandable Panels by Buddha (@jbuddha) on CodePen.

The panel can be minimized or maximized by clicking on the arrow button. It can also be removed completely by clicking close button. How do we achieve this? I have used KnockoutJS, OracleJET and jQuery to achieve the result. RequireJS is also used but only to get the required libraries from CDN. However, OracleJet is predomantly is used for styling alone. Rest of the bindings can be achieved by regular KnockoutJS and jQuery. Read forward to learn how to get the above result.

Read More

Breadth First Traversal In a Binary Tree Without Recursion

445236781246189461789

Breadth First Traversal of A Binary Tree
## The Problem

Imagine you have a binary tree where as shown above. You may be aware of InOrder traversal where you follow a scheme of visiting left subtree and then visit root node and finally visit right subtree. With small variations in order same is done in pre-order as well as post-order traversal. How do you do a breadth first traversal? It is slightly more tricky. Doing it non-recursively is even more difficult at first sight. Let me first explain what is breadthfirst traversal.

Different traversals produce different output as shown below

In-Order Traversal
1
1 12 23 46 89 445 461 678 789
Post-Order Traversal
1
1 12 89 46 23 461 789 678 445
Pre-Order Traversal
1
445 23 12 1 46 89 678 461 789
Level-Order Traversal
1
445 23 678 12 46 461 789 1 89

Read More

Restricting an ojInputText to accept only numbers

Oracle Jet is a beautiful toolkit for simplifying lot of tasks. ojInputText is a basic editor the framework provides, it can validate the text entered based on the regular expression we give, but validation only happens on blur and if we simply want to filter any keystrokes that don’t match that, we can’t do it by default.

Here is the result before we go and learn how to do it.

See the Pen Allow only numbers in OjInputText by Buddha (@jbuddha) on CodePen.

Ofcourse we can use ojInputNumber and use the example they gave for eating non-numbers, but what if we don’t want the increment and decrement the arrows of ojInputNumber. One way to do it is to bind a keyUp event and check everytime a character is pressed. Infact this is the approach that is used for the example given in OracleJet cookbook.

Here is an alternative approach using ojInputText. Instead of bindng to value, we can bind to rawValue attribute. This ensures that the observable gets updated on every keystroke.

Read More

Generating the Jam Coins

The Problem

The Problem

Here is a moderate problem of CodeJam’s qualification round of 2016. The Jam Coins. Here is the description of the problem. You need to generate jamcoins of either 16 digits for small dataset or 32 digits for large datasets. Jam Coins follow the given rules…

  1. A Jam Coin is only made up of 1’s & 0’s of the required number of digits.
  2. It begins and ends with 1.
  3. If that interpreted from base 2 to base 10, it should not be a prime number in any of them.

For Small dataset, you need to generate 50 jam coins of 16 digits and for large dataset, you need to generate 500 jam coins of 32 digits following above rules.

Read More