• 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

Maximum of all subarrays of size k

Amazon Interview Experience – SDE Chennai

Memory Efficient LinkedList

Number of Islands BFS/DFS

flattens 2 D linked list to a single sorted link list

Sort an array according to the order defined by another array

Closed Parentheses checker

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Coin Collection Dynamic Programming

Common Ancestor in a Binary Tree or Binary Search Tree

Printing each word reverse in string

System Design: Designing a LLD for Hotel Booking

Add Sub Multiply very large number stored as string

Circular Linked List

Sort Stack in place

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Word Break Problem

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Print Power Set of a Set

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Count Possible Decodings of a given Digit Sequence

Code Chef PRGIFT Solution

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Flipkart SDET Interview Experience

Check Binary Tree is Binary Search Tree or not

Given a string, find the first character which is non-repetitive

Apriori algorithm C Code Data Mining

Leetcode: Merge Intervals

Copyright © 2026 · Genesis Framework · WordPress · Log in