Getting TestCase Based I/O Without Any Loop In Golang


Sometimes interview questions come with some twist like

Solve this problem without using any loops

So today I will explain how to solve general input/output (i/o) with test cases problem where special rule defines as saying “Do not use any Loop statement”. We’re going to solve this in Golang. When this type of problem was asked by the interviewer, the first thing that came to our mind(especially those who didn’t solve lots of problem-solving in his/her school/university life). The problem statement is given below that we will try to solve the problem without using any loop.

Note: This problem can be solved differently or different recursive implementation can be applied too.

Problem Statement

– We want you to calculate the sum of squares of given integers, excluding any negatives.
– The first line of the input will be an integer N (1 <= N <= 100), indicating the number of test cases to follow.
– Each of the test cases will consist of a line with an integer X (0 < X <= 100), followed by another line consisting of X number of space-separated integers Yn(-100 <= Yn<= 100).
– For each test case, calculate the sum of squares of the integers, excluding any negatives, and print the calculated sum in the output.

Note 1: Do not put blank lines between test case solutions.
Note 2: Take input from standard input and output to standard output.


– Do not use any Loop statement
– You may only use standard library packages
– There should be no output until all the input has been received.


Defining Main & standard library packages

package main

import "fmt"

Defining a global variable for storing results

var result []int

Defining main function

func main() {
    var testCaseNum int

    nTestCase(0, testCaseNum)

Recursively taking inputs using ReadN

Note: The function will finish when the test case and index value become equal and we’re printing the result array recursively using PrintN as Golang doesn’t have any built-in functions which print array without any loop (to my knowledge at least).

func nTestCase(idx int, testCase int) {
    if idx == testCase {
        PrintN(result, 0)
    var n int
    all := make([]int, n)
    ReadN(all, 0, n)  // reading array elements

    result = append(result, sumOfSquare(all, 0, n))

    nTestCase(idx + 1, testCase)

Calculating sum of squares of given integers, excluding any negatives.

func sumOfSquare(values []int, idx int, arrayLen int) int {
    if idx == arrayLen {
        return 0

    if values[idx] > 0 {  // ignoring negative numbers in the array
        return values[idx] * values[idx] + sumOfSquare(values, idx + 1, arrayLen)
    return sumOfSquare(values, idx + 1, arrayLen)

Reading integer array inputs recursively

func ReadN(all []int, idx, n int) {
    if n == 0 {
    if m, err := fmt.Scan(&all[idx]); m != 1 {
    ReadN(all, idx+1, n-1)

Outputting result integer array recursively

func PrintN(values []int, idx int) {
    if len(values) == idx {

    PrintN(values, idx + 1)

In conclusion, When we saw this type of problem for the first time, we thought it’s easy problem until we read the line Do not use any Loop statement in your solution and the problem becomes hard for newbie or not too much experience on problem-solving.

You can read our other blog posts Here

So now we all know how to tackle this type of problem. Stay tuned for our next blog post. Thank you.

Yml or Yaml for DevOps

As software engineers, we are always learning new tech stacks as we process our careers. Everyone who works on any short of software farm all came across a term called DevOps. As the name suggests, ...

Working with DocuSign, Authorization and Sending Document for Signature

DocuSign is a well known platform where users can send their document for signing via email or your app. I will try to show you how DocuSign authorize an user and how can we send a document to users ...

Worker Pool in Golang

Tags: #advance #topic #golang #goroutine #channels #workerpool #threadpool Often we end up with some work which is so time-consuming that if we’re able to assign, multiple person/worker, to do ...
java web application

Top 9 Practical Benefits Of Using Java Web Application

Java web application frameworks have one of the greatest and largest communities of software engineers in the world. Companies can build a team with the help of this community at ease. There are different communities to help beginners, intermediate as well as expert developers.

Read More