8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[HireRoo] コーディングテストの入出力問題に対応する方法

Last updated at Posted at 2023-04-09

概要 

転職活動や就職活動の中で、コーディングテストを受ける機会があるかと思います。
特に HireRoo でのコーディングテストでは、必ず入出力問題が出題されます。
しかし、初めてHireRooのテストを受けると、
どうやって構築したコードの結果を出力すればいいのか?
と迷うことがあるかもしれません。

私自身もこの問題に直面し、解決するまでに時間がかかりました。
そこで、本記事では HireRoo のコーディングテストで入出力を適切に処理する方法を解説します。

結論

出力系のメソッドを使用しないで、returnで値を返せば出力されます。

出力方法

2023年4月時点で、私が使用しているプログラミング言語を用いて、チュートリアルの問題を題材にした解答例をご紹介します。

問題(チュートリアルで出題されている問題)

問題の説明(内容)
solution 関数には引数として int 型の配列 arr が与えられています。
arr に含まれるすべての要素を2倍した結果を int 型の配列で返すプログラムを作成してください。

解答例

例1:

入力: arr = [1,2,3]
出力: [2,4,6]
説明: 1*2=2, 2*2=4, 3*2=6 なので [2,4,6] が答えとなる。
例2:

入力: arr = [0,-1,1]
出力: [0,-2,2]
説明: 0*2=0, (-1)*2=-2, 1*2=2 なので [0,-2,2] が答えとなる。

前提

2 ≦ len(arr) ≦ 10000

goでの解答例

package main

func solution(arr []int) []int {
    // TODO: Implement me!

    // makeの引数: make([型/type], [長さ/len], [容量/cap])
    // len():組み込み関数  / 返り値は、「バイト長」で行われる

    result := make([]int, len(arr))
    for i, v := range arr {
        result[i] = v * 2 
    }

    return result
}

PHPでの解答例

<?php
    function solution($arr) {
        // TODO: Implement me!
        if (!$arr) {
            return [];
        }

        foreach ($arr as $key => $row) {
            $output[$key] = $row * 2;
        }

        return $output;
    }
?>

参考資料

感想

もし今回の記事の内容が、どなたかのお役に立てれば幸いです。
表題とは関係ありませんが、現在、著者はGoを勉強中です。これまで静的型付け言語を扱った経験がなかったため、シンプルな問題でも書き方に悩み、思った以上に時間を要する事が多々あります。まだまだGo言語について学ぶべきことが多いと実感しています。精進あるのみですね!

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?