Friday, 31 July 2020

Do You Need To Submit Blogger Sitemap In Google Search Console

Google crawler should be able to index your blog posts eventually. You don't have to submit it. But it is recommended to submit your blogspot blog sitemap to Google Search Console. It might help you get your posts or pages indexed in Google quicker.
For this, you don't need any custom sitemap for Blogger blog. Sitemap is automatically generated by Blogger.

For posts:
http://blog-name.blogspot.com/sitemap.xml
http://custom-domain.com/sitemap.xml

For pages:
http://blog-name.blogspot.com/sitemap-pages.xml
http://custom-domain.com/sitemap-pages.xml

If you are using custom domain name, you need to use custom domain urls mentioned above. You can check urls for your blog in the browser & submit them in Google Search Console. If you don't have pages in your blog, then don't submit sitemap for pages.

Thursday, 30 July 2020

Use Notepad++ As Offline JSON Validator

You can find plenty of websites to validate & view JSON text. JSONLint is pretty popular. But I have had issues with them when file size goes beyond 3-4 mb. I mostly use this website to validate & parse JSON. It works well with big file size. So if you have a large JSON string to validate, it can be useful.
Now if you are on Windows & already using NotePad++, you can use it as an offline JSON validator tool. Just install this plugin. Now you can validate & see JSON text in tree view inside the text editor. It works well with large text also.

Wednesday, 29 July 2020

Difference Between Wait AND Park Methods In Java Thread

If you are familiar with thread dump or at least looked at it couple of times, probably you would have noticed "WAITING"  & "WAITING (parking)" states. So what's the difference?
In Java we have Objects.wait() and Unsafe.park() methods. Both of them will suspend the running thread & put it in waiting state. But these two methods work on different principles.
Object.wait() method works on monitor based synchronization. So that works well with "happens-before" relationship in Java. To bring back a waiting thread in runnable state, we will use Object.notify() method on the same monitor object. So when the thread comes back to runnable state, it will surely get updated values of the variables shared across multiple threads. JVM will make sure thread state is synchronized with main memory, but that is the additional overhead.
Unsafe.park() method takes the thread as an argument. To move back a parked thread to runnable state, we need to call Unsafe.unpark() method on the same thread. It works on permit basis. When Unsafe.unpark() is called, it will unblock the thread if it is already parked or will make sure the next park call on the thread is unblocked immediately. So its performance should be better as no synchronization is required with main memory. That's why thread pool in general (for example ExecutorService) use park method while waiting for tasks from the blocking queue.
As you can see, these use cases are different. If you have state that is shared across threads & you want to make sure one thread should wait for other thread before proceeding with the update, then you should go ahead with wait() and notify() methods. As an application developer, mostly you won't have to use park() method, it's too low level of an API.

Tuesday, 28 July 2020

Find Pattern And Its Anagrams In A Text

Previously I have written few posts related to pattern matching algorithms namely Rabin-Karp and KMP algorithms. So I thought of adding one layer of complexity on top of it. Now in addition to search a string, we need to find all its anagrams in the given text.
We can do it in O(n) time if we consider that character set in the given text would be of fixed length. For example, if we consider only English alphabet, we will have only 26 characters. You can check this link which explains it quite clearly. It uses Sliding Window technique. If you are familiar with it or have gone through Rabin-Karp Rolling Hash function already, then it is pretty easy to understand. Feel free to comment for any clarifications or if you have any other resource.

Monday, 27 July 2020

How Asynchronous Ajax Calls Get Executed In JavaScript Event Loop

I will keep this post short. There are tons of resources where you can find JavaScript and it's event loop architecture. I am here to clarify few things.
Let's take an example from this resource. It fairly explains the event loop & the event queue used by JavaScript engine. But the question is what happens when an async call is made. We know it will get pushed to event queue once the call is complete or timeout happens. But it is unclear which component does it or how it actually happens. In the resource above, that component is mentioned as "event table".
One basic thing first, Javascript engine is single threaded & the thread will run the event loop. So for example, let's talk about V8 JavaScript engine which empowers Google Chrome browser & Node.js. Now as mentioned in the resource, a setTimeout() function call will go to event table. But what does that mean?
Here is the thing. Every Javascript engine will have a host like browsers in most cases. Or in case of Node.js, it is the whole server app which is more than just V8 engine. Node.js has various C++ libraries to complement it. This host can have other threads that can handle things like doing Ajax call or setting a timer. And these threads are also capable of pushing the tasks once completed to the event queue used in JavaScript event loop.
For browsers, setTimeout() call or Ajax function call is part of Web API. For Node.js, we have C++ library like Libuv. They are outside of JavaScript engine, but can access the event queue. So even though, Javascript engine is single threaded, it can take advantage of the threads of its host.

KMP Algorithm Explained In Plain English

As I covered Rabin-Karp algorithm in my previous post, I just remembered one detailed explanation that I read quite a while back regarding KMP algorithm. As KMP is also a part of string searching algorithms, I thought of sharing that article here. I have seen other resources or articles with good enough explanations. But if you are new to pattern matching algorithm or confused about how KMP algorithm works, then you should consider reading this. Especially how you create a proper prefix (which is also a suffix) table and then use it to skip through the characters has been explained in details. There might be few typos here & there, but it is a good place to start for beginners. Just check the comments section for the typos mentioned.
P.S. I saw some abusing comments to a particular comment in that article 😕. Just skim through that.

Sunday, 26 July 2020

Simple Explanation Of Rabin-Karp Pattern Matching Algorithm

We can find a lot of resources that explains Rabin-Karp algorithm which is used for pattern matching. I am just trying to note down a few which can help us understand it clearly. I went through several of them. And I found this YouTube video to be really useful.
But if you need more understanding of Rolling Hash function after watching the video, you can read this. Actually it also explains this string searching algorithm in a simple manner. If you are a beginner, I would recommend you to go through the video first, then read the material. Anyways it's a short read. Hope that would help you clearly understand Rabin-Karp algorithm.
And if you stumble into any other resources which you think is more helpful to you, please feel free to comment.

Direct Conversion From Any Number Base To Another Base Explained

Let's consider that we need to convert a base 4 number to base 8 number. Base 4 digits: 0 1 2 3 Base 8 digits: 0 1 2 3 4 5 6 7 We should...