• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

November 14, 2014 by Dhaval Dave

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1
Example : 00001111
Output : 4Method 1 : in O(n) we can do it . with sequential search.

Method 2 : Binary search.

Yes binary search is useful, as data is sorted.
Just we need to change its conditions for 0 and 1.
lets have a look at Function firstOccurance.

firstOccurrance(int[] sortedBins) {
    int lower = 0;
    int upper = sortedBins.length - 1;
    while (lower <= upper) {
        int middle = 1 + (upper - lower) / 2;
        if (sortedBins[middle] == 0) {
            lower = middle + 1;
        } else {
            upper = middle - 1;
        } 
        if (sortedBins[lower] == 1) {
            return lower;
        }
    }
    return -1;
}

Similar Articles

Filed Under: Adobe Interview Questions, Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Array, Binary Search

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

The Magic HackerEarth Nirvana solutions Hiring Challenge

Skiing on Mountains Matrix

Connect n ropes with minimum cost

C++ OOPs Part1

Find the number ABCD such that when multipled by 4 gives DCBA.

simple sql injection

Right view of Binary tree

Get K Max and Delete K Max in stream of incoming integers

Calculate price of parking from parking start end time prices

Circular Linked List

Memory Efficient LinkedList

Urban Ladder Written Test.

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

LeetCode : Word Search

Best Java Book | Top Java Programming Book for Beginners

Level order traversal in Spiral form

Apriori algorithm C Code Data Mining

Subset Sum Problem Dynamic programming

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Naurki.com Security Breach

Facebook Interview Question : Interleave List

SAP Off Campus Hiring_ March 2015 Sample Questions

Find and print longest consecutive number sequence in a given sequence in O(n)

Edit Distance ( Dynamic Programming )

Find if a binary tree is height balanced ?

Sequence Finder Dynamic Programming

DFS (Depth First Search)

Stock Buy Sell to Maximize Profit

Microsoft BING Interview Experience

Find Nearest Minimum number in left side in O(n)

Copyright © 2026 · Genesis Framework · WordPress · Log in