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

  • Home
  • SEARCH
  • 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 6532483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:00:31+00:00 2026-05-25T10:00:31+00:00

I was trying to solve random coding kata’s and found this one, my question

  • 0

I was trying to solve random coding kata’s and found this one, my question here is what is the optimal algorithm and best design approach for solving this kata?

Given a sequence of numbers, determine the type of sequence, calculate and return the next number in
the sequence.

Integer guessNextNumber(List<Integer> sequence);

The given sequence can be one of two types, arithmetic sequence and geometric sequence.

Arithmetic sequence is defined as:
Arith_seq(p,q) = p, p+q, (p+q) +q, …
Example: Arith_seq(7,3) = 7, 10, 13, 16, 19, …

Geometric sequence is defined as:
Geo_seq(p,q) = p, p*q, (p*q) * q, …
Example: Geo_seq(2,3) = 2, 6, 18, 54, …

Expected input and output:
The input sequence will have at least 3 numbers.
For the input sequence (7, 10, 13, 16, 19), the return value would be 22.
For the input sequence (2, 6, 18, 54), the return value would be 162.

Algorithm:

  1. Check input sequence
    if we have sequence as (a, b, c) then,
  2. if difference between elements of sequence (b-a) or (c-b) is
    equal then its Arithmatic Sequence.

  3. if division between elements of sequence is equal, eg: b/a and
    c/b then its Geometric Sequence

My question what would be an optimal algo for solving it?

Update: Is it possible to solve this constant run time?

  • 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-25T10:00:32+00:00Added an answer on May 25, 2026 at 10:00 am

    Lets say your collection is a,b,c,d

    • b - a + b = c then it is arithmetic
    • b / a * b = c it is geometric

    To return the right sequence you could thus do

    • d – c + d = e and if e – d = d – c return e
    • else
    • d / c * d = e and if e / d = d / c return e

    Java would be (assuming it is either artihmetic or geometric, never anything else, and always atleast 3 entries)

    public int nextInt(int[] s){
      if( s[1] - s[0] == s[2] - s[1] ) return ( s[1] - s[0] ) + s[s.length - 1];
      return ( s[1] / s[0] ) * s[s.length - 1];
    }
    

    Safe code would be

    public int nextInt(int[] s){
      if(s!=null && s.length > 3){
        if( s[1] - s[0] == s[2] - s[1] ) return ( s[1] - s[0] ) + s[s.length - 1];
        if( s[1] / s[0] == s[2] / s[1] ) return ( s[1] / s[0] ) * s[s.length - 1];
      }
      return -1;
    }
    

    With List instead of Array

    public Integer guessNextNumber(List<Integer> sequence){
      if( sequence.get(1) - sequence.get(0) == sequence.get(2) - sequence.get(1) ) return (    sequence.get(1) -  sequence.get(0) ) +  sequence.get(sequence.size() - 1);
      return ( sequence.get(1) / sequence.get(0) ) * sequence.get(sequence.size() - 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to solve a puzzle which i found here: http://zcasper.blogspot.com/2005/10/google-phone-interview.html the goal is to
I was trying to solve my XNA Font problem , when I found this
Been trying to solve this one for hours... string date = 2009-09-23T13:00:00 DateTime t
Trying to solve this problem . I would like to learn how the bootstrapper
In trying to solve the ajax back button problem I have found the Really
I'm trying to solve this flickering problem on the iphone (open gl es game).
im trying to solve this issue. I have a web site with this simplified
Been trying to solve this for a while now. I need a regex to
I have been trying to solve this problem for three days now, it's really
I'm trying to use the Simple hill climbing algorithm to solve the travelling salesman

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.