Skip to main content

Featured Post

Salesforce Agentforce Specialist Certification: What It Takes in 2026

 With Agentforce now central to Salesforce's roadmap, demand for people who can actually configure, ground, and govern AI agents has spiked — and Salesforce's certification track has changed to match. If you're weighing whether to get certified in 2026, here's what the credential actually covers and whether it's worth your time. The Certification Landscape Changed in 2026 Salesforce retired the older AI Associate and AI Specialist certifications in early 2026. The current credential is the Salesforce Certified Agentforce Specialist (exam code AI-201 ) — a single, more practical exam replacing the older, more theory-focused ones. What the Exam Actually Tests Rather than abstract AI theory, the Agentforce Specialist exam focuses on real configuration and governance skills: Designing AI workflows using prompts, actions, data, and automation. Grounding agents with relevant, trustworthy data. Building and customizing agents for specific business needs. I...

Binary Search Explained Step by Step (with JavaScript Code)

 Binary search is usually the first "real" algorithm people learn in DSA, and for good reason: it's simple, it's fast, and it teaches the core idea behind a huge number of more advanced algorithms — solve a big problem by repeatedly cutting it in half.


The Problem It Solves

You have a sorted list of numbers and you want to know if a target value exists in it (and where). The naive approach — checking every element one by one — is O(n). Binary search does it in O(log n), which is dramatically faster for large lists.


How It Works

  1. Look at the middle element of the list.

  2. If it matches the target, you're done.

  3. If the target is smaller, repeat the search on the left half.

  4. If the target is larger, repeat the search on the right half.

  5. Keep going until you find it, or the range becomes empty (not found).

Every step throws away half the remaining list — that's why it's so fast. Searching 1,000,000 sorted items takes at most ~20 comparisons.


JavaScript Implementation

function binarySearch(arr, target) {
  let low = 0;
  let high = arr.length - 1;

  while (low <= high) {
    const mid = Math.floor((low + high) / 2);

    if (arr[mid] === target) {
      return mid; // found it
    } else if (arr[mid] < target) {
      low = mid + 1; // search the right half
    } else {
      high = mid - 1; // search the left half
    }
  }

  return -1; // not found
}

const sortedNums = [1, 3, 4, 6, 7, 8, 10, 13, 14, 18, 19, 21, 24];
console.log(binarySearch(sortedNums, 14)); // 8

Why the Array Must Be Sorted

Binary search only works because a sorted array guarantees that everything to the left of the middle is smaller, and everything to the right is bigger. That guarantee is what lets you safely discard half the list every time. Run it on an unsorted array and it will give you wrong answers.


Common Interview Variations

  • Find the first or last occurrence of a repeated value.

  • Search in a sorted array that's been rotated.

  • Find the smallest value that satisfies a condition ("search on the answer").


Conclusion

Binary search turns an O(n) problem into an O(log n) one just by using the fact that the data is sorted. Once this pattern clicks, you'll start spotting places to apply it well beyond plain array searching. Next in this series: Sorting Algorithms Explained.

Image: AlwaysAngry / Wikimedia Commons (CC BY-SA 4.0)

Comments

Popular posts from this blog

Understanding SQL Query Execution Order

When writing SQL queries, understanding the execution order is crucial for writing efficient and optimized code. Many beginners assume that queries execute in the order they are written, but in reality, SQL follows a specific sequence of execution. SQL Execution Order SQL queries run in the following order: 1️⃣ FROM + JOIN 2️⃣ WHERE 3️⃣ GROUP BY 4️⃣ HAVING 5️⃣ SELECT (including window functions) 6️⃣ ORDER BY 7️⃣ LIMIT Let’s break down each step with examples. 1. FROM + JOIN (Data Retrieval) The SQL engine first retrieves data from the specified table(s) and applies any JOIN operations. 🔹 Example: SELECT employees.name, departments.department_name FROM employees JOIN departments ON employees.department_id = departments.id; Here, the JOIN happens before any filtering ( WHERE ) or grouping ( GROUP BY ). 2. WHERE (Filtering Data) Once data is retrieved, the WHERE clause filters rows before aggregation occurs. 🔹 Example: SELECT * FROM employees WHERE salary > 50000 ; Thi...

Top 5 React.js Performance Optimization Techniques for 2025

React.js continues to dominate the front-end development landscape due to its flexibility, component-based architecture, and performance. However, as applications grow, performance issues can emerge, affecting user experience. Here are the top five performance optimization techniques every React developer should consider in 2024. 1. Use React.memo for Component Memoization React.memo is a higher-order component that prevents unnecessary re-renders by memoizing the result. It only re-renders when props change, improving performance for functional components that rely on the same data. import React from 'react';  const ExpensiveComponent = React.memo(({ data }) => {      console.log('Rendering ExpensiveComponent');      return <div>{data}</div>;  }); export default ExpensiveComponent; 2. Implement Code Splitting with React.lazy and Suspense Code splitting reduces the initial load time by splitting the code into smaller bundles. Re...

Best Practices for Securing Personal and Business Data in 2025

In today’s digital landscape, cybersecurity is more critical than ever. With increasing cyber threats, data breaches, and privacy concerns, individuals and businesses must take proactive steps to secure their data. This guide outlines the most effective security practices for 2025. 1. Implement Strong Authentication Measures Passwords alone are no longer sufficient to protect sensitive accounts. Instead, consider: ✅ Multi-Factor Authentication (MFA): Require users to verify their identity using an additional factor, such as an SMS code, authenticator app, or biometric authentication. ✅ Passkeys & Password Managers: Use passkeys where available and store strong, unique passwords in a secure password manager. 2. Encrypt Sensitive Data Encryption ensures that even if data is stolen, it remains unreadable without the decryption key. 🔹 Use end-to-end encryption (E2EE) for messages and emails. 🔹 Encrypt stored data on cloud services, external drives, and local machines. 🔹 Consider ...