Thursday, October 10, 2024

How to Check if array is a binary heap in Java?

Problem : You have given an array of integer, check if the given array represents a binary max-heap data structure. In max heap every node is greater than all of its descendants. 


For example :

Input: array[] = {100, 25, 20, 17, 22, 12} 

Output: true

The given array represents below tree

100

/ \

25 20

/ \ /

17 22 12 

This binary tree follows max-heap property as every node is greater than all of its descendant. 


Input: array[] = {19, 25, 20, 17, 22, 21} 

Output: false

This array represents following tree :

19

/ \

25 20

/ \ /

17 22 21

This tree violates max-heap property as root 19 is less than 25 and 20, and 20 is smaller than 21.



Solution :

The brute force solution of this problem is first to check if root is greater than all of its descendants and then subsequently check for all children of root. Time complexity of this solution is O(n^2) because for each node you check every other node. 


You can optimize your solution by only comparing root with its children and not with all descendants. If root is greater than its children and same is true for all nodes then tree is a max-heap. This solution is similar to preorder traversal of binary tree because there also we compare root, left and right node. 


Here is a Java solution of problem, how to check if a given array is binary max heap or not :


/**

* Returns true if given array is a binary max-heap. false otherwise

* @input[] - given array

* @i - start index

* @length - length of array

*/


public static boolean isBinaryMaxHeap(int i, int[] tree) {

// Base case

if (i >= tree.length - 1)

return true;


// If root is greater than its children and

// same is recursively true for all the children, then tree is binary

// max heap


int ROOT = i;

int LEFT_CHILD = 2 * i + 1;

int RIGHT_CHILD = 2 * i + 2;


if (valueOf(ROOT, tree) > valueOf(LEFT_CHILD, tree)

&& valueOf(ROOT, tree) >= valueOf(RIGHT_CHILD, tree)

&& isBinaryMaxHeap(LEFT_CHILD, tree)

&& isBinaryMaxHeap(RIGHT_CHILD, tree)) {

return true;

}


return false;

}


Time Complexity of this solution is O(n) because we only check 2 other nodes for each node.


    Tuesday, October 8, 2024

    Top 7 Books and Online Courses to Crack the PMP Certification Exam in 2025 - Best of Lot

    If you are in the Project management and People management area, then you might have heard about PMP certifications, one of the most reputed and sought-after IT certifications for project managers and people aspiring to start their career in project management. PMP is a short form of Project Management Professional and is offered by the Project Management Institute (PMI). As I said, it's one of the most reputed certifications for project management professionals. There is an immense global demand for project managers. Several leading and best companies hire certified project managers to manage their projects. According to a PMI report, over 2 million project management roles need to be filled every year.

    Monday, October 7, 2024

    Top 5 Courses for CompTIA A+ Certification in 2025 (with Practice Test) - Best of Lot

    Hello guys, CompTIA A+ is a famous certification among beginners who wants to start a career as an IT professional and learn information technology in general. This IT certification doesn't have any requirement to take it and it is recognized in all companies around the world and includes some topics such as information security, network administrator, computer repair, operating systems, and many more computer fundamental topics. If you don't know what is CompTIA let me give you a brief overview. CompTIA stands for Computer Technology Industry Association.

    20 Best Coursera Professional Certificates for IT Professionals in 2025 [UPDATED]

    Hello guys, If you are thinking of a career in Information Technology and Software Development in 2025 but lack a Computer Science degree or a Professional degree then you don't need to disappoint. Coursera's professional certificates can give you all the knowledge and credential to start a career in Information Technology. If you don't know, Coursera launched Professional Certificates recently which can help you get job-ready for an in-demand career field in less than a year. You can earn a career credential, apply your knowledge to hands-on projects that showcase your skills for employers and get access to career support resources.

    Saturday, October 5, 2024

    10 Code Review Checklist and Best practices in Java

    What to review while doing Code review
    Code Review and Unit testing are some of the best development practices I always recommend, strive for, and enforce as much as possible. Even just by doing code review and Junit test case always offer positive results it can be improved a lot by constantly learning from our mistakes, others mistakes and by observing how others are doing it. I always try to get my code review done by someone with more technical and domain experience so that I can capture any domain-specific scenarios which have been missed during think through the process.

    How to find the first element in Stream in Java 8? findFirst() Example

    In Java 8, you can use the Stream.findFirst() method to get the first element of Stream in Java. This is a terminal operation and is often used after applying several intermediate operations e.g. filter, mapping, flattening, etc. For example, if you have a List of String and you want to find the first String whose length is greater than 10, you can use the findFirst() method along with stream() and filter() to get that String. The stream() method gets the Stream from a List, which then allows you to apply several useful methods defined in the java.util.Stream class like filter(), map(), flatMap() etc.

    Top 10 Data Science Certification Courses for Python and R Developers in 2025 - Best of Lot

    Hello guys, Data Science, Machine Learning, Deep Learning, and Artificial intelligence are really hot at this moment and offering a lucrative career to programmers with high pay and exciting work. It's an excellent opportunity for programmers who are willing to learn these new skills and upgrade themselves. It's also important from the job perspective because Robots and chatbots are getting smarter day by day, thanks to these technologies, and most likely will take over some of the jobs which many programmers do today. Hence, it's essential for software engineers and developers to upgrade themselves with these skills.

    Top 5 Online Courses to Learn Artificial Intelligence in 2025 - Best of Lot

    If you want to learn Artificial Intelligence in 2025 and looking for the best online courses then you have come to the right place. Earlier, I have shared the best Data Science courses and today, I am going to share the best courses to learn Artificial Intelligence in 2025. These are the best online courses for Artificial Intelligence or AI for beginners and curated from sites like Udemy, Pluralsight, and Coursera, three of the best online learning platforms. These courses have been created by experts like Andrew Ng, an AI pioneer and founder of Coursera, and trusted by thousands of programmers and non-technical people who want to learn AI.  You can also join them to learn Artificial Intelligence in 2025. 

    Top 10 Coursera Certifications, Courses, and Specializations to Learn Essential Skills in 2025 - Best of Lot

    Hello guys, if you are looking for the best Coursera courses, certification, and specialization to join in 2025 then you have come to the right place. In the last few articles I have shared the best Coursera courses to learn PythonSoftware Development, and Cloud Computing, and today, I will share the best Coursera courses, specializations, and certifications you can join in 2025. Taking online courses and certifications have become mandatory in order to have a professional and successful career and the process is similar to a college education just by staying at your home taking classes watching videos and passing the quizzes and done you are now officially certified and when it comes to the best platform to learn online no one can compete with Coursera.

    Friday, October 4, 2024

    10 Examples of Optional in Java 8

    Null is bad, it can crash your program. Even its creator called it a billion-dollar mistake hence you should always try to avoid using nulls whenever you can. For example, you should not return a null String when you can return an empty String, similarly never return null collections when you can return an empty collection. I have shared many such tips in my earlier article, 10 tips to avoid NullPointerException and my reader liked that a lot. But, I wrote that article a couple of years ago when Java 8 was not around and there was no Optional, a new way to avoid NullPointerException in Java, but, things have changed now.

    Why use SLF4J over Log4J for logging in Java? Example

    Every Java programmers know that logging is critical for any Java application, especially server-side application, and many of them are already familiar with various logging libraries e.g. java.util.logging, Apache log4j, logback, but if you don't know about SLF4J, Simple logging facade for Java,  then it's time to learn and use SLF4J in your project. In this Java article, we will learn why using SLF4J is better than using log4j or java.util.logging. It’s been a long time, since I wrote 10 logging tips for Java programmer,I don’t remember anything I have writing about logging.

    Top 10 Coursera Courses and Certifications to Learn Web Development in 2024 - Best of Lot

    Hello guys, If someone asked me 10 years ago about how to learn web development and get a job I would more likely say that you have to go to college and get your Bachelor’s degree and then try to get a job or internship. In short, it was only possible to become a web developer by spending years on education and thousands of dollars, now the internet has changed the game nowadays. These days many online platforms like CourseraUdemyPluralsight, CodeCademy, and Educative appeared to the world allowing people to take online training courses in almost any industry and web development is one of them. 

    Top 6 Courses to Learn IntelliJIDEA and Android Studio IDE for Java and Kotlin Programmers in 2024 [Best & FREE]

    There is no doubt that IntelliJ IDEA is THE best IDE for Java development, even though Eclipse may still be probably used by more people because it's FREE, IntelliJ IDEA is the most feature-rich and complete IDE. The Android Studio, which is the official IDE for Android development in Java, is also based upon IntelliJ IDEA, which further cement its place as the IDE Java developer should learn. The only thing which stops many other Java developers and me from moving from IntelliJ IDEA in the past was the lack of resources. Since Eclipse was free from the start, there are tons of resources, like books, courses, and tutorials are available. 

    Top 5 Free Courses to Learn Kubernetes for Developers and DevOps Engineers in 2024 - Best of Lot

    Hello guys, DevOps is becoming an essential skill in today's Programming and Software Development world and Kubernetes is an important concept and tool for DevOps engineers. It takes container-based deployment to another level and allows you to manage it on the scale. You can use Kubernetes to scale your container environment or let Kubernetes do all the work for you by leveraging its auto-scaling feature. Many DevOps beginners think that Docker and Kubernetes are the same but they are not. Docker provides a container to deploy your application and commands to interact with those but container but Kubernetes is actually a container management technology, which decides how many containers are needed and deal with other management aspects.

    Thursday, October 3, 2024

    How to replace NULL with Empty String in SQL Server? ISNULL() vs COALESCE() Examples

    We often need to replace NULL values with empty String or blank in SQL e.g. while concatenating String. In SQL Server, when you concatenate a NULL String with another non-null String the result is NULL, which means you lose the information you already have. To prevent this, you can replace NULL with empty String while concatenating. There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.