Tuesday, January 14, 2025

From Human Touch to Machine Algorithm: The Price of Progress

 In an age where progress is measured by economic growth, technological advancements and possessions, there is a silent transformation happening. Humans are increasingly becoming mechanical in their thought processes, treating every interaction and decision as a business transaction. This shift is visible in many facets of modern life, from corporate practices to individual choices. While we often talk about the risks of artificial intelligence replacing humans, a more pressing issue lies in how humans themselves are becoming machines.

1. Profitable Layoffs: The Business of Efficiency

In recent years, companies have redefined "efficiency" to mean "doing more with fewer people." Layoffs are no longer a last resort for struggling businesses but a calculated strategy to maximize profits. Even companies with soaring revenues and record-breaking profits are cutting jobs, citing "streamlining operations." This isn’t about survival; it’s about greed disguised as growth.

What message does this send to society? That people are dispensable, reduced to numbers on a balance sheet. The human cost of these layoffs—mental health issues, family instability, loss of dignity—is rarely considered. When profit margins dictate decisions, humanity takes a backseat.

2. The Salary Chase: Employees as Free Agents

On the other side of the coin, employees are equally complicit in perpetuating a transactional culture. Job loyalty has dwindled as professionals hop from one company to another in search of higher salaries. The days of building a career with one organization are fading fast.

But can we blame them? When companies treat employees as expendable assets, it’s natural for individuals to prioritize their financial security. However, this constant churn creates a culture where relationships, mentorship, and long-term impact are sacrificed at the altar of immediate gains.

3. The Death of Local Markets: A Race to the Bottom

The convenience of online shopping has revolutionized consumer behavior. Yet, beneath the allure of lower prices lies a harsh reality. Local retailers, who once formed the backbone of communities, are being edged out by global giants. These giants often offer cheaper products at the expense of ethical practices, environmental sustainability, and local economies.

Every time we click "Add to Cart," we contribute to a system that prioritizes cost over conscience. Do we really want globalization to mean that an International corporation monopolizes a developing market, erasing centuries-old local businesses in its wake? If this trend continues, what will be left of our neighborhoods, our culture, our individuality?

4. The Self-Checkout Dilemma: Convenience at a Cost

Self-checkouts at grocery stores and mobile order pickups at coffee shops are celebrated as marvels of efficiency. They save us time and reduce costs for businesses. But what happens to the cashier who loses their job? What about the barista who no longer interacts with regular customers?

These technologies, while convenient, strip away the human connections that make everyday interactions meaningful. They also reinforce a culture where speed and cost-cutting take precedence over community and empathy.

5. Profit Over People: The New Normal

Whether it’s corporate decisions, personal choices, or social interactions, everything seems to revolve around profit. Friendships are built on networking potential. Hobbies are monetized. Even acts of kindness are weighed against what one stands to gain in return.

This relentless focus on profit isn’t just dehumanizing; it’s unsustainable. A society that values profit over people loses its soul, becoming a cold, calculated machine where compassion and ethics are anomalies rather than norms.

6. The Bigger Problem: Humans Becoming Machines

While the world debates the implications of AI, we’re overlooking a bigger issue: humans turning into machines. We’ve become so obsessed with optimization, efficiency, and profitability that we’ve lost touch with what makes us human—empathy, creativity, and the ability to connect deeply with one another.

When we reduce life to a series of transactions, we strip it of meaning. What’s worse, we’re passing these values down to our children. We’re teaching them that success is about saving money, maximizing returns, and cutting corners—ethics and morals be damned. Is this the legacy we want to leave behind?

Final Thoughts

I’ll admit, I often struggle with these same choices. Sometimes, I choose the cheaper online option. Sometimes, I rely on self-checkouts. But these moments leave me wondering—what are we losing in the process?

It’s not about pointing fingers or finding quick fixes. It’s about reflecting on the kind of world we’re creating. Can we balance progress with humanity? Can we teach the next generation that life is more than just a series of profit-driven decisions?

I don’t have all the answers. But perhaps, by asking the right questions, we can start a conversation worth having.

Saturday, January 11, 2025

Lessons from a Payment System Built Using Python: Insights from a Java Architect

 

Lessons from Building a Payment System Using Python: Insights from a Java Architect

As a seasoned Java developer and technical architect, I recently had the opportunity to work on a complex payment system built primarily with Python. The experience was an eye-opener and a journey of learning. While Python’s strengths were evident in several aspects of the system, it also highlighted areas where its inherent nature posed challenges, especially when compared to my prior experiences with Java. This post is a reflection on that journey and a quest as to: could such a system be organized more effectively while maintaining Python’s advantages.


What Worked Well with Python

1. Smooth Integrations with External Components

One of Python’s standout strengths is its ability to integrate seamlessly with external systems. Whether connecting to ACH processors, payment gateways, or KYC services, Python’s extensive libraries and ease of API handling makes the process remarkably smooth. The ability to quickly prototype integrations is a significant productivity booster.

2. Powerful Data Analysis Capabilities

In the payment domain, analyzing card and user data is critical for fraud detection, recommendation engines, and user behavior insights. Python excelled in these areas with its rich ecosystem of data-focused libraries like Pandas, NumPy, and machine learning frameworks. These tools allowe us to deliver robust analytical capabilities quickly.

3. Rapid Development

Python’s simplicity inherently means faster implementation of APIs and business logic. This helps particularly in delivering features under tight deadlines, a common scenario in the competitive payment industry.


Challenges Encountered

1. Lack of Structure in Function-Based Design

Python’s dynamic and function-oriented nature, while flexible, introduces challenges in maintaining code quality. Without strict conventions, the codebase risks becoming a dumping ground for quick fixes and unstructured logic. This is especially true in collaborative environments with varying levels of developer experience.

For new team members, the challenge is compounded by large, unstructured files that could be overwhelming to navigate. Effective documentation and mentoring are crucial in such scenarios to ensure smooth onboarding and to help new developers understand the flow of the system.

But let's all agree, documentation is bound to get outdated and become even more misleading. 

2. Database Query Complexity

Writing database queries directly in Python often results in scattered logic and inconsistent practices. While libraries like SQLAlchemy provide some structure, they fall short compared to the robustness of Java’s Spring Data JPA. 


What I Learned and How I Would Organize Such a System

1. Imposing Strict Development Standards

To mitigate the risks of unstructured code, it is essential to enforce coding standards and best practices rigorously:

  • Adopt a Layered Architecture: Even in Python, structuring the application into clear layers—controller, service, repository—can help maintain separation of concerns.

  • Use Linters and Type Annotations: Tools like Flake8 and mypy can enforce coding standards and provide type safety to some extent.

2. Using ORM Effectively

While Python’s ORMs like SQLAlchemy are powerful, they require discipline to avoid query sprawl. A few strategies include:

  • Centralized Query Management: Keep queries in separate repository classes to promote reusability and consistency.

  • Leverage Query Builders: Use SQLAlchemy’s query-building features for complex queries, avoiding raw SQL unless absolutely necessary.

4. Fostering Documentation and Mentorship

To address the challenges posed by unstructured codebases and large files, I’ve come to appreciate the critical role of documentation and mentorship:

  • Comprehensive Documentation: Maintain clear and concise documentation that explains the purpose and flow of major components.

  • Mentorship Programs: Encourage senior team members to guide new developers, providing them with the context and knowledge to navigate complex systems effectively.

  • Onboarding Guides: Develop step-by-step guides for new hires to help them get up to speed quickly.

5. Leveraging the Best of Both Worlds

Instead of an all-Python approach, a hybrid model could leverage the strengths of different languages:

  • Core Payment Processing: Use Java for transaction-heavy components where strong typing, thread management, and performance are critical.

  • Integrations and Analytics: Retain Python for its rapid development and data analysis capabilities.

  • Common Standards Across Languages: Define cross-language architectural standards to maintain consistency.


Conclusion

Working on this Python-based payment system was a humbling and enriching experience. It reinforced my belief in the importance of choosing the right tool for the job and the need for disciplined development practices, regardless of the language.

While Python’s flexibility and ease of use were clear advantages, they also highlighted the importance of imposing structure and standards to avoid technical debt. At the same time, I’ve come to appreciate Python’s strengths in areas like integrations and analytics, which complement Java’s robustness in core processing.

This experience also emphasized the value of comprehensive documentation and mentorship in helping new team members navigate complex systems effectively. I aim to apply these lessons to build more balanced and maintainable systems, combining the best features of Python, Java, and other languages.

 Ultimately, the goal is not to favor one language over another but to create systems that are resilient, scalable, and adaptable to the ever-evolving demands of the payment industry.

Thursday, January 26, 2017

Top 5 places to visit in Nova Scotia - in 3 days


Travelling. It leaves you speechless. Then turns you into a storyteller. - Ibn Battuta

An Evening in Bay of Fundy - Nova Scotia

There are certain places in the world, the beauty of which cannot be described with images of any resolution or words of any language. You simply have to witness them through your own eyes. Nova Scotia is one such place.

Inspiration:


Every province in Canada has a lot to offer. A lot more than you can cover in one trip. Nova Scotia with its miles long coastline along the majestic Atlantic ocean, wildlife and breathtaking trails is no exception to that rule.This maritime province is flooded with natural beauty, greenery and culture. If you are a photography enthusiast, you would not have time to breath.

When it comes to choosing a destination for my next travel plan, I gravitate towards one with a lot of natural beauty to explore. Definitely while in Canada, Nova Scotia was bound to be on my list.I shared this wish with a few of my friends who shared the same enthusiasm about travelling to places and boom!!, we were ready to fly. But considering the fact that we could only use long weekends (a weekend with either Monday or Friday as a holiday) for our travel plans, we had a tough call to make. We had to choose what to see from the long list of must see attractions in Nova Scotia. Here's a quick list for your reference:


Must see attractions:

  1. Halifax
  2. Peggy's Cove
  3. Lunenburg
  4. Bay of Fundy
  5. Cape Breton Highlands National park
Looking at the list superficially, one may think that the list is small, but let me stop you right there. These are just names. Each of it is a reservoir of  beautiful attractions and not to miss sites/activities in itself. Nova Scotia is one such place where any trip will feel short. But probably a weeks trip should be good if you would like to experience Nova Scotia as a traveller.


Must do activities:

  1. Whale watching
  2. Tidal bore rafting 
  3. Witnessing the world's highest tides in Bay of Fundy.
  4. Cabot Trail
  5. Hiking/biking/kayaking/canoeing 


Elimination Round/choosing what we can afford to miss:


Each and every place listed above offers a unique experience and hence is kind of a must. It was nearly impossible to play favorite between them. But then we had limited money and time and exactly one driver. This helped us narrow down our choices. We had to come up with a set of places we could cover in one trip spanning 3 days and 4 nights including the time to travel from Toronto to Nova Scotia.

The Province of Nova Scotia can be broadly seen as two parts: The mainland and the Cape Breton island. While Halifax, Peggy's Cove, Lunenburg and Bay of Fundy are located on the mainland, the island is home to the great Cabot Trail. Both of them cannot be covered in a single 3 day trip. Its either mainland or the island. We chose mainland because it involved less driving and we were particularly fascinated by the Bay of Fundy.

If you choose to go to Mainland, there are flights from Toronto Pearson airport as well as the Billy Bishop Island airport to Halifax. If you are planning to go to the Cape Breton Island instead, the better option would be to choose to land in Sydney (Nova Scotia not Australia).

Our Itinerary

Our Nova Scotia Itinerary

What's Interesting


The Flight


We were excited even before we reached Halifax as we had chosen to start from the iconic Billy Bishop (BB) Island airport. This is the beautiful island airport easily accessible from downtown Toronto. The flight was decent but really enjoyable for us because it was first time for all of us to fly from BB.

Billy Bishop Airport Toronto and CN tower in the shadow 😊

Halifax ( Morning to late night)


No matter if you are a strolling kind like me, a night owl, no actually a bat (Bat is my brand ambassador), or a history buff, you will find plentiful in Halifax.

They call it a walk-able city. After living in a big city like Toronto for a while when we visited Halifax, it felt charming and it was indeed walk-able. We experienced most of Halifax on our feet. After having an early stomach full breakfast buffet at the downtown hotel we stayed at, we headed for the tour on foot. Strolling around the city downtown, we visited the old town clock, the Citadel and other buildings around downtown before we had an Indian buffet lunch. I don't remember what we had in lunch though. Forgive me for that. Will ya?

The Old Town Clock

As the Park's Canada site says, the Citadel delivers history with a bang! The noon gun is a not to miss attraction. I don't know at this moment how I can share the heavy video on this blog. (An action item for me)

The bats have no reason to be disappointed either. The Lower Deck is just a place to go. Here you can meet locals, dance to the ever running stream of loud music and enjoy a local brew.

After the relaxing day at Halifax, we headed for the next big thing. Peggy's cove.

Peggy's Cove (3 Hours)


Peggy's cove - Most photographed in North America - rightly so.

The treacherous tides of the deep blue ocean crashing on the beautiful rocks at the Peggy's cove will keep you stunned for hours. The roughly three hours that we spent here felt like minutes. I don't have more words to describe it and hope that the picture says it all.

If you have visited a lighthouse by the ocean before, you know exactly what to expect. Having said that, when you visit this one, you will know why this is the most photographed place in North America.

I had something at the Sou'wester Restaurant which probably did not go as expected. I think it was boiled salmon and I am not a seafood lover. Still I tried it because I was not sure when I will get another chance of having a fish by the Atlantic. But the famous Nova Scotia Haddock Fish and Chips would mostly be a good try if you are a seafood lover. One of us had it and I guess enjoyed it.


Lunenburg (Roughly 1 day)


Lunenburg with its colourful houses

This UNESCO world heritage site is known for its vibrant and colourful homes. Driving through it's narrow streets overlooking beautiful houses and farmer's markets we reached our destination - the whale watching centre.

The whale watching experience is absolutely breathtaking, weather you see a whale or not. Experiencing the waves miles into the ocean when you cannot see any shore has no match from land. The waves that we peacefully appreciate from the shore take the form of giant beasts with tremendous power. You can feel the overwhelming power of a wave when it gulps your entire boat carrying at least 40 people and throws you right back as if it does not care for your tiny existence. And yes, all this when the weather report says that the ocean is calm. The thought that how tiny and vulnerable you are haunts you. But after all when you see how comfortably the whales of a size of a truck roam around and how the seagulls coexist with them, you are rendered speechless.


Whales playing in the ocean

After this amazing experience, on our way to Bay of Fundy, we grabbed all the food we would need for this and the next day. It's hard to find anything at the Bay.


Bay of Fundy (Plan to be at the bay in the evening, early morning and noon)


This one is my personal favorite and at least according to me the star of the trip. One of the 7 wonders of North America and home to the highest tides in the world, it is also one of the natural wonders of the world. This is the place where you can get a feel of the daily movements of the mother earth.

Every 6 hours the bay fills and empties billions of tonnes of water due to the tidal effects. This causes the sea bed to be exposed and immersed in deep water every 6 hours. While at times you can walk on the sea bed, at other times you may have to take a boat.

The Bay of Fundy is large and there are a lot of places on the bay where you can stay.

We had booked Shangri-La cottage at one such location which was at the heart of all the water activity. All I can say about this cottage is that I would definitely recommend it to who ever consults me.

The Shangri-La cottage we stayed at

After spending an extremely relaxing evening at the camp fire by the cottage by the most amazing bay on earth, we woke up early in the morning to see the tidal effects at 6 AM while the sea bed was still visible and then at noon when the bay was almost filled with the water and had already started getting empty for the next emptying session. At this moment you can observe waves coming from both directions. An effect similar to the one seen when we shake a glass of water.

Bay of Fundy- Half way empty

Bay of Fundy - Full

After relaxing in the afternoon, we snail speed drove on the serpentine roads with no one in site except miles of farmlands, sheep and occasional scarecrows to the Shubenacadie river. I guess you can do tidal bore rafting around Shubenacadie. If you have time, I guess there are a lot of activities you can do here.We just stayed there for some time, did some hiking and headed back to the Halifax airport saying good bye to the wonderful times and the province of Nova Scotia.


What's left


Cape Breton island with attractions like the world famous Cabot Trail, the Fortress of Louisbourg, wildlife and innumerable hiking trails is definitely a must go and is on my list already. It has a lot of unique adventure to offer. I will create an Itinerary for it when I have experienced it.

Please let me know if this itinerary was helpful and I will share more such itineraries as I travel more.

As usual, comments/appreciation/criticism (a constructive one of course) everything is most welcome and appreciated.

Until then,
Happy Learning!
Banyan Bat

Saturday, January 14, 2017

What I learnt from Steve not Steve Jobs

Something that's hard to get unless on Facebook 😊

A couple of years ago, some of my friends and I were in Mumbai, India. One of these friends had just bought a new car and he was excited about it. He wanted to take us on a ride. Largely to show his new car I would guess. He is also the kind who would go into the most glamorous store, dream about how he would choose the best product in that store and walk out of the store, of course buying exactly nothing.

So, he got into his car along with us of course, put his seat belt sunglasses on, played a rock song on the custom music system and headed to one of the newly constructed malls. Learning a lot of features about the car and the custom features installed just for my friend, we reached the mall.

Strolling in the mall, we came across a Bose showroom. "Lets go and have a look. What say guys?" the enthusiastic one insisted and rest of us followed.

I don't clearly remember the name of the guy who greeted us, lets call him Steve. After the last post, that's what came to my mind first. Informing us that he is there to assist us if anything needed he let us go on our own.

After looking at all the wonderful and grossly out of budget products from Bose, while we were about to leave, I witnessed something that would teach me a key business principle and change my opinion for Bose forever.

Steve: Sir, we have a demo for the best product form Bose and I would like you to see it.

     All of us in chorus: Well, no. Its Okay.

Steve: Why not? It will take only 10 minutes or so and you would enjoy it.

     Someone among us: No, Really its Okay! We won't be able to buy it anyway.

Steve: That's okay sir, not today, but may be tomorrow you will.

     All of us in chorus again: Okay.

We listened to the demo of a home theatre system from Bose that would roughly cost about $5000 or INR 3,00,000. A product that would probably suit a room that matches its standards. A product that we would probably not buy in our life time. (At least not me. And definitely I am not going to under estimate my friends. Rather I wish they buy such a system if they wish to). We thanked Steve for a wonderful demo of the product and we left the store.


"Treat the customer with dignity and respect, even if he or she chooses not to do business with you. Finish Strong!" -  Shep Hyken

I think Steve justified the above quote and even went an extra mile in doing that as none of us were existing customers and probably did not even show any signs of remotely thinking of becoming one ever or probably he was simply trying to finish his target. I crossed that later thought intentionally because I would like to think that the farmer is true and is one of the real keys to success.

What this whole incident has done is that it has put a permanent mark on my mind saying out loud: Bose is great!! If you ever get so much money, don't think twice and go buy a Bose product.

Okay, but what was in it for Steve? Steve must have inherently felt good after we left happily. More importantly, he has created a tiny source of positive vibrations for him for the rest of his life. And in doing so, he could not stop himself from helping Bose leave a lasting impression.

On that note I would like to finish this post.

As usual, comments/appreciation/criticism (a constructive one of course) everything is most welcome and appreciated.

Until then,
Happy Learning!
Banyan Bat

Monday, January 9, 2017

The one Step you need to win a customer for life - and how Apple did it



"You have got to start with the customer experience and work back towards the technology - not the other way round." - Steve Jobs

"One customer well taken care of could be more valuable than $10,000 worth of advertising." - Jim Rohn


Recently while I was working on my 4 year old Macbook, it suddenly got sick and would not boot up. [Okay, I was watching a movie, but still it was work for my computer, eyes and brain].
I tried a couple of times with no luck. Immediately I went to my phone and said: "OK Google, Macbook pro not starting with a folder and question mark".

The first result I got was the Apple support site with exactly what I was looking for: a solution to the problem I just told Google. I read through all the possible solutions and unfortunately none worked. The only solution that applied to my situation read something like this: "Your mac may need a repair. Please book an appointment at your nearest Genius Bar". I thought, great, its a nice excuse to hit a bar for a chilled pour. But to my disappointment, Apple calls its tech support station a Genius bar.

Me - "Looks like its time to buy a new laptop." My last laptop went on ventilator early in its 4th year and died soon after. This one was also in its 4th year, so that was a reasonable assumption. I also thought may be, I would not go for a mac again, after all its costly. Those were scary and sad moments. But before that, I had to give it a try for its repair.

As suggested by Google, I booked an appointment at the nearest Genius bar. The appointment was 4 days later. Without a laptop, days are slow :). During the days we were waiting for the appointment laptoplessly, I was suggested that I should go for a local repair store instead as they may charge less as compared to the coveted brand store. But I thought, lets give it a try at the store, ask them for a quotation and then decide what to do.

Finally the day of my appointment came. I had an appointment at 5, just after the end of my workday. Usually that's just fine, but Murphy's law kicked in. I had some urgent work to complete in the office at the same day and I missed my appointment by just 15 mins. When I reached the Genius Bar, it was crowded with a long queue full of people waiting for their turn. These were the people who get a chance when people like me screw up. I reached out to one of the associates at the Genius bar and asked if I can be squeezed in. As I had already guessed, he suggested otherwise, and the next available appointment was in the next week. I got disappointed by the thought of waiting for another week. But then he suggested that we could try another apple authorized store which is less crowded considering that it does not have the big logo on its front door.

Without wasting any time, I rushed to that store. When I reached the service desk, I was greeted with a welcome just like any other store. Then I told my problem quickly and handed over the laptop. The service desk associate ran some diagnostic tests and told me that it would be a couple of minutes before she can tell me whats wrong. Those 2 minutes were long. (In my head: oh man, whats f*#%ed?, how many dollars will I have to pay?, will all my data be lost?, will I have to buy a new laptop? I cannot afford a new Macbook again :() And then the service desk associate said, she believes she knows the problem. I said, oh, what is it? "The laptop is not booting because the cable connecting the motherboard with the hard drive is dysfunctional." I said okay, but is it repairable?
She said: "Of course, and you know what, you would not have to pay a penny as both the diagnosis and the repair for this particular problem are covered by Apple." She continued: "Apple recognizes that quite a few customers who bought laptops in mid 2012 faced this issue and hence, apple decided to bear the cost."

That was relaxing. More than that it was enriching.  Immediately, I felt pampered. All the thoughts that had crossed my mind a minute ago including the thought that I would not go for a mac again were gone and I was happy. Instantaneously I decided that eventually when I do have to buy a new computer, it would be nothing else but mac again.

It was a four years old mac, I personally don't think Apple had any obligation towards it. They could have easily charged me for the diagnosis as well as the new cable. I think most of the other companies would do the same and that would in no sense be unfair. After all the laptop had functioned optimally for last four years. But Apple decided to go the extra mile and honour the repairs even after the warranty period.

It's probably not a big deal for a giant like Apple to do such small services for free. But at the same time, being such a success could have possibly made them think that they don't need to care for such small details. I believe that it takes courage and dedication to do such small things and these simple and small things are the ones which give big rewards and make a difference between success and average, let alone failure.

All this is on top of the fact that the original product is the best. The mac as a product is surely unbeatable. Be it hardware, software, the presentation or the fine details, mac beats most others on each and every aspect.

So, probably the key to success is to build your craft, refine it day and night, pay attention to detail and most importantly, stay humble.

As usual, comments/appreciation/criticism (a constructive one of course) everything is most welcome and appreciated.

Until then,
Happy Learning!
Banyan Bat

Monday, January 2, 2017

Binary Insertion sort - Getting speed

"Please lord, let me prove that winning in the casino won't spoil me." - Bubbly
After we helped Bubbly arrange her cards, she was rocking in the casino. She is now a winning sensation in the Venetian. But as her game has improved, so has her need to be faster and smarter with her cards.

So far as I remember, we had used the fastest way we knew to get her cards sorted. But now with her improved game and competition, that seems slower and we need to find even faster ways to get her cards in order.

Is there a way we can achieve this? Lets try to find out by carefully examining how the Insertion sort works.

As you must have noticed in the post Bubbly goes to Las Vegas, insertion sort works by taking one element at a time, searching its sorted position in already sorted partial array and putting it there. To search this position, we iterated over all the elements in the partially sorted portion linearly and compared the element at hand with each element until we got the desired sorted position for this element.

Let me highlight a few keywords in the above statement : insertion sort works by taking one element at a time, searching its sorted position in already sorted partial array  and putting it there.

If you are following this blog, which I wish you were but I am sure you are not,  you would remember from the post You are searching so hard..., that our study for sorting started because searching in a sorted array is faster.

As can be seen, the insertion sort as of now searches for the element position by simple iteration and comparison which does not take into consideration the fact that we know the arrangement of numbers in the already sorted array. This type of search is called a linear search. This is fine for a small array. But for a larger array this can turn out to be slower as irrespective of the position of the to be searched element in the array, we will iterate through all the elements until we find the one we are seeking. So what is it that we can do so that this search will happen faster?

Binary search for rescue:

I am quite sure, most of you already know Binary search as well. Have you ever played the game "Guess the number", where you ask your friend to pick up a random number and tell them that you will  guess it? Those who answered yes instantly already know the Binary search algorithm. And for others, A quick refresher.

Lets assume that Bubbly and her friend Silly are playing this game.

Bubbly: Hey Silly, wanna play a game?

     Silly: Of course, I am game for a game. Always!

Bubbly: Ok Its a game of numbers.

     Silly: Maths?? :( No way.

Bubbly: Don' worry, you don't have to do any math. You Just have to pick a number between 1 to 10 and I will tell you what number you picked.

     Silly: That sounds fun.

Bubbly: I will guess the number but if its incorrect, I will ask you some hint questions and I would expect you to be honest with me.

     Silly: Fair enough!

Bubbly: Ok so lets get started. Pick a number between 1 to 10 and don't tell me the number. :)

     Silly: umm.. (7)...ok I did.

Bubbly: OK. Let me guess!!.. ummm Is it 5?

     Silly: Nope :).

Bubbly: Okay, is it less than 5 or greater than 5?

     Silly: Greater. As if she is ever gonna be able to tell me what I picked. HAHAHAHA

Bubbly: Okay! Is it 8?

     Silly: Nope, no no.

Bubbly: Okay, is it less than 8 or greater than 8?

     Silly: Less. Oh man, how is she closing in on the number?

Bubbly: Okay, is it 6?

     Silly: Nope. You are never going to find it.

Bubbly: Great! Actually I have found it. It is 7.

     Silly: No Way? How did you guess?

Bubbly: La lala lalalalalala....

Okay, So as you can see, that was fast.

What Bubbly did was the efficient use of Binary search algorithm.

Here's a snapshot of what was going on in Bubbly's head:




Instead of looking through each element of an array linearly, binary search leverages the knowledge that the array is sorted and looks for the desired item by comparing it with the element at the middle of the array. If the element matches, all good, otherwise, we can simply ignore half of the array based on whether the number we want is greater or smaller than the one at the middle of array. This is efficient because, after each comparison, we are eliminating half of the comparisons that we would have done if it were a linear search.

Which means, if we are sorting a considerably large array, insertion sort can be made even faster by doing a binary search when searching for the ideal position for next element in array. The only thing that we need to consider is, mostly the element we are searching won't be present in the partially sorted array and hence, instead of saying that the element was not found, our search should return the position where it would have ideally been found. And in case of a non existent element, that will happen when we reach the array of size 1.

Such an insertion sort algorithm that uses binary search to search for an ideal position for each element while searching is called binary insertion sort.

I hope this gave you a clear idea of how Binary search helped us achieve a search algorithm which is slightly faster than the plane old insertion sort.

As usual, comments/appreciation/criticism (a constructive one of course) everything is most welcome and appreciated.

Until then,
Happy Learning!
Banyan Bat

Saturday, September 24, 2016

Killing the Loop in a Link

In the last post we discussed a way to find the loop in a link. Once we know that there is a loop in our linked list, first thing we should do if we want our linked list to be realistic and usable, is to remove the loop.

How do we kill the beast? I guess if we know where the loop starts, we will be able to burn its tell (set the next pointer of the magic node to null).

Lets get started.

What we already know? 

From our little stint in the last post in this category, we know for sure that there is a loop in the below linked list.
Linked List with a Loop
We also have our hare and tortoise pointers pointing to an element inside the loop. Like so.


As the hare and tortoise story goes, the hare stops in between the race thinking that the tortoise can never catch or overtake him. Eventually the tortoise through its sheer perseverance catches hare, overtakes him and wins the race. Nice inspirational story.

In our case, the ends don't meet as we don't have an end to our linked list. And by the way, our hare has read that story and thus he never stops. Which means he will continue to loop crossing the tortoise again and again until we let him stop. Which also means that at this point when both hare and tortoise are pointing to the same node, if we let our hare run and at each node increment loopsize by 1, by the time hare catches tortoise back, we will have the size of our loop.


What we already know? Update 1.
  • That there is a loop in our linked list.
  • And the size of the loop (in this case 4). 
Now lets start at the beginning again. Set hare and tortoise both to point to the start of the linked list and then give our hare a heads up of exactly the loopsize. (Yeah, this time no matter what, we will let our hare win the race. Why should tortoise have all the fun?). This makes hare and tortoise loopsize apart - make a mental note of it, this will come handy.

What we already know? Update 2.
  • That there is a loop in our linked list
  • The size of the loop
  • And two pointers one pointing at the start of the linked list and another loopsize away from it.
Now advance both hare and tortoise exactly by one node until both point to the same node at which point, tortoise will be at the start of the loop and hare will be at the end of the loop. And yeah, its a loop so both start and end are same :). Also while you are advancing the hare pointer, keep track of the previous value, something like this:
previous = hare;
hare = hare.next;


What we already know? Update 3.
  • That there is a loop in our linked list
  • The size of the loop
  • The start of the loop.
  • The Element at the end of the loop that points back to start.
Now killing this loop is simple. As I said earlier, just burn the tail. Okay, in technical terms, doing something like previous.next = null (removing the red pointer in the above figure); should do the trick. Here's our final linked list without a loop in it.


I hope you enjoyed this post. And I was able to express the algorithm in a way that will stick to your grey matter.

As usual, comments/appreciation/criticism (a constructive one of course) everything is most welcome and appreciated.

Until then,
Happy Learning!
Banyan Bat

P.S.

After successfully understanding the above algorithm to find and eliminate a loop in a linked list, you have an important and very handy tool under your belt sometimes referred to as a Two Pointer Algorithm. This algorithm can be easily used in a lot of other problems. One such example is:
  • Find Nth node from the end of the linked list.
I would like to leave you with the problem to find a solution. You can google for a ready made algorithm to solve this problem, but if you think on the grounds of what we just discussed in the last two posts, it might stick around your head a bit longer.

Also, this post pretty much concludes our discussions related to the singly linked list data structure. Although we will come back to the topic as and when it feels like a right thing to do :).