3

I had an interview recently and was asked to solve the following problem using Python:

Write a function:

def solution(A):

such that, given an array A consisting of N integers, it returns the maximum among all one-digit integers.

For example, given array A as follows:

[-6, -91, 1011, -100, 84, -22, 0, 1, 473]

the function should return 1.

Assume that:

  • N is an integer within the range [1...1,000]
  • Each element of array A is an integer within the range [-10,000..10,000].
  • There is at least one element in array A which satisfies the condition in the task statement.

I came up with the following solution which I thought was elegant, but the score I got was only 55%:

def solution(A):
    return max([i for i in A if len(str(i))==1])

Why is this solution incorrect?

12
  • 7
    -6 is one digit. But len(str(-6)) is 2? Commented Sep 16, 2021 at 1:22
  • 6
    Also, any interview question that tells you to call a Python local variable capital A was definitely written by someone who doesn't actually use Python. Commented Sep 16, 2021 at 1:23
  • 8
    Also converting an integer to a string is not that elegant. You could have used if -9 <= i <= 9. Commented Sep 16, 2021 at 1:23
  • 2
    @ZhubeiFederer Not the original commenter, but in my view not only is this terrible style (non-descriptive in nature) but variables beginning with a capital letter are stylistically reserved for class names. Commented Sep 16, 2021 at 1:36
  • 5
    @ZhubeiFederer - per pep8, local variables should be lower_case_with_underscores and they should be descriptive. A isn't either. Commented Sep 16, 2021 at 1:36

2 Answers 2

4

Here is a solution that ticks more points in my book.

def solution(integers):
    """Return max single digit integer in sequence `integers`"""
    try:
        return max(i for i in integers if -9 <= i <= 9)
    except ValueError:
        return ValueError("Could not find single digit integer in list")

print(solution([-6, -91, 1011, -100, 84, -22, 0, 1, 473]))

It uses a descriptive parameter name, has a comment, does a simple comparison for digit size and uses a generator with the built in function max instead of the needless list. It also reraises the most expected fault with a more topical error message. This function is easier to maintain and use.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the logic I went this, although as Selcuk has pointed out, max(i for i in a if i < 10) is a cleaner approach.
That doesn't work for large negative numbers. Did you mean abs(i)? That was my original solution but I changed to the comparison under the shakey assumption that the comparison is faster.
Why wouldn't it work for large negative numbers? Please provide an example of an array it wouldn't work with?
@sprogissd [-11, 33] would report -11 as a 1 digit number.
4

Not the most elegant. This assumes that e.g. -3 is single digit.

This algorithm will play by the rules set. Considering the constraints:

  • The number is -10,000 to 10,000
  • There is at least one element which satisfies the condition

Then we can just guard all numbers beyond 9 to be the lowest -10,001 so that they are not considered to be max. We don't need to care for numbers lower than -9 as we are sure there is at least 1 element (that is a single digit whether positive or negative, either way greater than those numbers lower than -9) that will satisfy the condition.

print(
    max(
        [-6, -91, 1011, -100, 84, -22, 0, 1, 473, 5, 4, 67, 7, 3, 56],
        key=lambda num: -10001 if num > 9 else num
    )
)
print(
    max(
        [-6, -91, 1011, -100, 84, -22, 10, -5, -8, 67],
        key=lambda num: -10001 if num > 9 else num
    )
)

Output:

7
-5

2 Comments

Good thinking. You could also simply use max(i for i in a if i < 10).
I haven't thought of that! Yes indeed, that is the better (if not best) version that plays by the rules :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.