InterviewBit WORST problem definition ever (Scala solution attached)


#1

Solving this problem has been an escalation of frustrations.
I get that we need to ask questions to the interviewer to narrow down / specify the use cases but here the job was made simply impossible.

Basically a treasure hunt to find the next UNDECLARED (and some done as a pretext to have) weird use-case.

All my points gone in discovering the next UNASKED use - case

def atoi(A: String): Int = {

  val chars = A.toCharArray

  def parse(index: Int, sign: Int, acc: Int, inNumber: Boolean): Int = {
    if (index == chars.length) sign * acc
    else
      chars(index) match {
        case ' ' if !inNumber => parse(index + 1, sign, acc, inNumber) // ignore blank before we meet a number
        case '-' => parse(index + 1, -1, acc, true)
        case '+' => parse(index + 1, sign, acc, true)
        case d if d.isDigit =>
          val result: Long = acc * 10L + (d - '0')
          if (result > Int.MaxValue) {
            if (sign > 0) Int.MaxValue else Int.MinValue
          } // overflow
          else parse(index + 1, sign, result.toInt, true)
        case d if !d.isDigit && acc == 0 => 0
        case _ =>  sign * acc
      }
  }

  parse(0, +1, 0, false)

}