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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:54:05+00:00 2026-05-26T04:54:05+00:00

I need to find the next available ID (or key) from a fixed list

  • 0

I need to find the next available ID (or key) from a fixed list of possible IDs. In this case valid IDs are from 1 to 9999, inclusive. When finding the next available ID we start looking just after the last assigned ID, wrap around at the end – only once, of course – and need to check if each ID is taken before we return it as an available ID.

I have some code that does this but I think it is neither elegant nor efficient and am interested in a simpler way to accomplish the same thing. I’m using Ruby but my question is not specific to the language, so if you’d like to write an answer using any other language I will be just as appreciative of your input!

I have elided some details about checking if an ID is available and such, so just take it as a given that the functions incr_last_id, id_taken?(id), and set_last_id(id) exist. (incr_last_id will add 1 to the last assigned ID in a data store (Redis) and return the result. id_taken?(id) returns a boolean indicating if the ID is available or not. set_last_id(id) updates the data store with the new last ID.)

MaxId = 9999

def next_id
  id = incr_last_id

  # if this ID is taken or out of range, find the next available id
  if id > MaxId || id_taken?(id)
    id += 1 while id < MaxId && id_taken?(id)

    # wrap around if we've exhausted the ID space
    if id > MaxId
      id = 1
      id += 1 while id < MaxId && id_taken?(id)
    end

    raise NoAvailableIdsError if id > MaxId || id_taken?(id)

    set_last_id(id)
  end

  id
end

I’m not really interested in solutions that require me to build up a list of all possible IDs and then get the set or list difference between the assigned IDs and the available IDs. That doesn’t scale. I realize that this is a linear operation no matter how you slice it and that part is fine, I just think the code can be simplified or improved. I don’t like the repetition caused by having to wrap around but perhaps there’s no way around that.

Is there a better way? Please show me!

  • 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-05-26T04:54:06+00:00Added an answer on May 26, 2026 at 4:54 am

    Since you’ve already searched from incr_last_id to MaxId in the first iteration, there isn’t really a need to repeat it again.

    Searching from 1 to incr_last_id on the second round at least reduces the search to exactly O(n) instead of a worse case of O(2n)

    If you want to do it in a single loop, use modulo,

    MaxId = 9999
    def next_id
      id = incr_last_id
      limit = id - 1 #This sets the modulo test to the id just before your start point
      id += 1 while (id_taken?(id) && (i % MaxId) != limit)
      raise NoAvailableIdsError if id_taken?(id)
      set_last_id(id)
      id
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the need to find the next available number in a set: select
I need to find the next three available business days for a scheduling application.
I need to find a way to spin off a thread from a static
I need to find 2 elegant complete implementations of public static DateTime AddBusinessDays(this DateTime
I have the following problem: We need to find the next august. I other
Given two positive integers x and y, I need to find the next number
Possible Duplicate: bit twiddling: find next power of two How to obtain the next
Need to find the timestamp for the first minute of the first day of
I need to find a way to call a vb.net function in my aspx
I need to find/create a library that can load hdr images in many formats

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.