Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:06:29+00:00 2026-06-01T08:06:29+00:00

This was asked in my Google interview recently and I offered an answer which

  • 0

This was asked in my Google interview recently and I offered an answer which involved bit shift and was O(n) but she said this is not the fastest way to go about doing it. I don’t understand, is there a way to count the bits set without having to iterate over the entire bits provided?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:06:30+00:00Added an answer on June 1, 2026 at 8:06 am

    Brute force: 10000 * 16 * 4 = 640,000 ops. (shift, compare, increment and iteration for each 16 bits word)

    Faster way:

    We can build table 00-FF -> number of bits set. 256 * 8 * 4 = 8096 ops

    I.e. we build a table where for each byte we calculate a number of bits set.

    Then for each 16-bit int we split it to upper and lower

    for (n in array)
       byte lo = n & 0xFF; // lower 8-bits
       byte hi = n >> 8;   // higher 8-bits
       // simply add number of bits in the upper and lower parts 
       // of each 16-bits number
       // using the pre-calculated table
       k += table[lo] + table[hi];
    }
    

    60000 ops in total in the iteration. I.e. 68096 ops in total. It’s O(n) though, but with less constant (~9 times less).

    In other words, we calculate number of bits for every 8-bits number, and then split each 16-bits number into two 8-bits in order to count bits set using the pre-built table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i was asked in an interview about this question. I did google a
I asked this question in the restkit google group, but realize now that it
I found this on Google, click here , which someone asked a similar question,
I know that this question asked a lot (i think) but i could not
I asked this question on the Google Group but I think I will get
I thought this was asked before, but 15 minutes of searching on Google and
I asked this over on Google's support forums for Apps integration, but got zero
I have already asked this question in offical Flot google groups, but got no
I recently asked this question on here and got the answer. However I'm now
I have asked this question in Radiant CMS google group, but no one answered

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.