[Solved] Golang Brain Storming Interview Coding Question with Solution

Introduction

In this tutorial, we will look at Golang brain storming interview coding question with solution. I have experienced that in interviews, coding problems are mostly focused on testing your ability to solve a problem using some data structure along with your approach to pick up a suitable solution for a given problem. We will look at one such coding problem in go which is mostly asked in interviews.

 

[Solved] Golang Brain Storming Interview Coding Question with Solution

Golang Brain Storming Interview Coding Question with Solution

We will solve the given question in four programming languages namely, Go, Python, Java and C. Let us first look at the problem and understand what does it expect and then we will look at the solution.

Also read: [Solved] How to Upgrade Go Version in Linux

 

Problem Statement:

Given denominations for coins being [1,5,10,50,100,500]. How many total coins are needed for [0,12,468,123456]?
Example – For 12 you need 3 coins, for 468 you need 10 coins.

 

Golang Solution:

For the given problem above, your job is to find the minimum number of denominations (from the given set of denominations ) needed to represent a given value. I have provided the solution below along with how the solution works.

package main

import (
    "fmt"
)

func findDenom(arr []int, val int) int {

    sum := 0

    for i := len(arr) - 1; i >= 0; i-- {
        if val >= arr[i] {

            // Calculate how many times the denomination can be used.
            numCoins := val / arr[i]

            // Add the number of coins to the sum.
            sum += numCoins

            // Update the remaining value.
            val -= numCoins * arr[i]
        }
    }
    return sum
}

func main() {

    // Define the array of denominations.
    arr := []int{1, 5, 10, 50, 100, 500}

    // Define the input values for which we want to calculate the total coins needed.
    inputValues := []int{0, 12, 468, 123456}

    // Calculate the total coins needed for each input value.
    for _, val := range inputValues {

        totalCoins := findDenom(arr, val)
        fmt.Printf("Total coins needed for %d: %d\n", val, totalCoins)
    }
}

OUTPUT

PS C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump> go run .\findDenom.go
Total coins needed for 0: 0
Total coins needed for 12: 3
Total coins needed for 468: 10
Total coins needed for 123456: 253

 

Let us understand how the code works.

1. The ‘findDenom‘ function takes two arguments, an array of denominations (‘arr’) and a value (‘val‘).

2. Inside the function, it initializes a variable ‘sum‘ to zero. This variable will keep track of the total number of denominations used.

3. The function then enters a loop that iterates through the denominations in descending order (from largest to the smallest).

4. For each denomination, it checks if the current denomination (‘arr[i]’) is smaller than the remaining value (‘val‘).

5. If the denomination is smaller than the remaining value, it calculates how many times the denomination can be used to represent the value (‘val / arr[i]’) and adds this count to the ‘sum’.

6. It then updates the remaining value (‘val’) by taking the remainder (‘val % arr[i]‘) after using the denomination.

7. The loop continues until it has considered all denominations.

8. Finally, the function returns the ‘sum’, which represents the minimum number of denominations needed to represent the original value.

In the ‘main’ function, the code defines an array of denominations (‘arr‘) and calls the ‘findDenom‘ function with a value of ‘val’. The result is printed, showing the minimum number of denominations needed to represent ‘val’ using the provided denominations.

 

Java Solution

public class Main {
    public static int findDenom(int[] arr, int val) {

        int sum = 0;

        for (int i = arr.length - 1; i >= 0; i--) {
            if (val >= arr[i]) {

                // Calculate how many times the denomination can be used.
                int numCoins = val / arr[i];

                // Add the number of coins to the sum.
                sum += numCoins;

                // Update the remaining value.
                val -= numCoins * arr[i];
            }
        }
        return sum;
    }

    public static void main(String[] args) {

        // Define the array of denominations.
        int[] arr = {1, 5, 10, 50, 100, 500};

        // Define the input values for which we want to calculate the total coins needed.
        int[] inputValues = {0, 12, 468, 123456};

        // Calculate the total coins needed for each input value.
        for (int val : inputValues) {

            int totalCoins = findDenom(arr, val);
            System.out.printf("Total coins needed for %d: %d%n", val, totalCoins);
        }
    }
}

 

Python Solution

def find_denom(arr, val):

    sum_coins = 0

    for i in range(len(arr) - 1, -1, -1):
        if val >= arr[i]:

            # Calculate how many times the denomination can be used.
            num_coins = val // arr[i]

            # Add the number of coins to the sum.
            sum_coins += num_coins

            # Update the remaining value.
            val -= num_coins * arr[i]

    return sum_coins

def main():

    # Define the array of denominations.
    arr = [1, 5, 10, 50, 100, 500]

    # Define the input values for which we want to calculate the total coins needed.
    input_values = [0, 12, 468, 123456]

    # Calculate the total coins needed for each input value.
    for val in input_values:
        total_coins = find_denom(arr, val)
        print(f"Total coins needed for {val}: {total_coins}")

if __name__ == "__main__":

    main()

 

C Solution

#include <stdio.h>

int findDenom(int arr[], int size, int val) {

    int sum = 0;

    for (int i = size - 1; i >= 0; i--) {
        if (val >= arr[i]) {

            // Calculate how many times the denomination can be used.
            int numCoins = val / arr[i];

            // Add the number of coins to the sum.
            sum += numCoins;

            // Update the remaining value.
            val -= numCoins * arr[i];
        }
    }
    return sum;
}

int main() {

    // Define the array of denominations.
    int arr[] = {1, 5, 10, 50, 100, 500};
    int size = sizeof(arr) / sizeof(arr[0]);
    // Define the input values for which we want to calculate the total coins needed.
    int inputValues[] = {0, 12, 468, 123456};
    int numInputValues = sizeof(inputValues) / sizeof(inputValues[0]);

    // Calculate the total coins needed for each input value.
    for (int i = 0; i < numInputValues; i++) {

        int val = inputValues[i];
        int totalCoins = findDenom(arr, size, val);
        printf("Total coins needed for %d: %d\n", val, totalCoins);
    }
    return 0;
}

 

Summary 

We solved one of the most important brain storming code using Go, Python, Java and C. If you do not have go runtime configured in your system. You can practice the solution in go playground which is free and online available for use.

 

Leave a Comment