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 7797645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:45:02+00:00 2026-06-01T23:45:02+00:00

Hey I had this question in an interview and was wondering what was the

  • 0

Hey I had this question in an interview and was wondering what was the best way to solve it. So say you are given an array that is already sorted and you want to find the lowest index of some value x.

Here is a python/pseudocode of what I came up with, I am just wondering if there is a better way to go about it?

def findLowestIndex(arr, x):
     index = binarySearch(0, len(arr), x)
     if index != -1:
         while index > 0:
           if arr[index] == arr[index-1]:
              index -= 1
           else:
              break
     return index

Thanks!

  • 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-01T23:45:04+00:00Added an answer on June 1, 2026 at 11:45 pm

    Your method takes linear time in the worst case, which is when the count of xs in the array is O(n).

    An O(lg n) solution can be obtained by changing the binary search itself to find the first x in the array instead of just any one of them:

    def binary_search(x, a):
        lo = 0
        hi = len(a)
    
        while lo < hi:
            mid = (lo + hi) // 2
    
            if a[mid] < x:
                lo = mid + 1
            elif a[mid] > x:
                hi = mid
            elif mid > 0 and a[mid-1] == x:
                hi = mid
            else:
                return mid
    
        return -1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. Quick question on Fluent syntax. I had thought I had this down,
Hey this is one of the classes that I had for homework in my
Hey this might be a simple question, but I have never had to import
Hey everyone, I had a quick question that should be easy for someone who
Hey, Since Google Fonts came out, I have had this question in mind. First
Hey guys i had an idea yesterday. Can you help me with this. Here
Hey This is my login script, using PHP5 and MYSQLi, I just had help
Hey guys, I haven't used the rpg pattern before, and had a quick question.
hey, this i have a loop that adds gathered strings and ints into an
Hey guys this question is a bit nebulous so I apologize...I'm just looking for

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.