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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:51:20+00:00 2026-05-19T14:51:20+00:00

I have taken this example test before I’ll try to take the real one

  • 0

I have taken this example test before I’ll try to take the real one for a job interview. The challenge is to implement the Equilibrium Index problem correctly.
I have coded some sort of solution that works only for the simple example and some of the edge cases.
Here’s the code:

typedef vector<int> container_t;
typedef container_t::const_iterator iterator_t;

int find_equilibrium(const container_t &A);

int equi (const container_t &A)
{
    const std::size_t length = A.size();
    if (length  == 0)
        return -1;

    if (length == 1 && A[0] == 0)
        return -1;

    if (length == 1 && A[0] != 0)
        return 0;

    return find_equilibrium(A);
}

int find_equilibrium(const container_t &A)
{
    unsigned int i = 0;
    int sum = 0;

    for (iterator_t iter = A.begin(); iter != A.end(); ++iter, ++i)
    {
        sum += *iter; // not using std::accumulate to avoid N^2

        if (sum == 0)
            return i + 1;
    }

    return -1;
}

It is however fatally flawed for cases like [2, -2].
It does however for some reason avoid arithmetic_overflow exception.
After Googling, I found the algorithm to be different then my own:

#include <numeric>
typedef vector<int> container_t;
typedef container_t::const_iterator iterator_t;

int find_equilibrium(const container_t &A);

int equi (const container_t &A)
{
    const std::size_t length = A.size();
    if (length  == 0)
        return -1;

    if (length == 1)
        return 0;

    return find_equilibrium(A);
}

int find_equilibrium(const container_t &A)
{
  unsigned int i = 0;
  int left = 0, right = std::accumulate(A.begin(), A.end(), 0);

  for (iterator_t iter = A.begin(); iter != A.end(); ++iter, ++i)
  {
    right -= *iter;

    if (left == right)
      return i;

    left += *iter;
  }

  return -1;
}

This code fails with extremely large numbers but works otherwise. I have no idea why though.

My questions are:

  • What’s wrong with the way I was approaching this and how can I approach a different question of this type more correctly within a time limit of 30 minutes?
  • What really are all the corner cases for this problem exactly?
  • How can I score 100 for this example test?
  • 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-19T14:51:21+00:00Added an answer on May 19, 2026 at 2:51 pm

    Your logic it’s right.

    A[0] + A[1] + A[2] = A[3] + A[4] + A[5] is equivalent to A[0] + A[1] + A[2] – (A[3] + A[4] + A[5]) = 0

    However, in your code, in the loop you add the values all the time, you never change the sign for those in the other half.

    for (iterator_t iter = A.begin(); iter != A.end(); ++iter, ++i) {
      sum += *iter; // only increasing, where do you subtract the values of the second half?
      if (sum == 0)
        return i + 1; // from the initial value you return the next index that gives you zero sum
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
So, take this html for example: <ul> <li> <a href=#>Test</a> <ul> <li><a href=#>Test</a></li> <li><a
How to convert string that have \r\n to lines? For example, take this string:
I have a Stored procedure which schedules a job. This Job takes a lot
I have taken an AI course, and the teacher asked us to implement a
I have found this example on StackOverflow: var people = new List<Person> { new
I have this Array i wrote a function MostFreq that takes an array of
So I have this code that takes care of command acknowledgment from remote computers,
Well basically I have this script that takes a long time to execute and
I have taken over a large code base and would like to get an

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.