Kotlin Interview Playground

Real Kotlin, compiled and executed on the official Kotlin compiler service.

Log in

Two Sum

HashMapArrays
Easy

Problem

Return the indices of the two numbers in nums that add up to target. Exactly one solution exists. Return them as a sorted pair, e.g. "0,1". Return "" if none.

Examples

Example 1basic
Input
twoSum(intArrayOf(2, 7, 11, 15), 9)
Output
0,1
Example 2middle pair
Input
twoSum(intArrayOf(3, 2, 4), 6)
Output
1,2
Example 3duplicates
Input
twoSum(intArrayOf(3, 3), 6)
Output
0,1
Example 4negatives
Input
twoSum(intArrayOf(-1, -2, -3, -4), -7)
Output
2,3
Example 5no answer
Input
twoSum(intArrayOf(1, 2), 99)
Output
"" (empty string)
Solution.kt

Your own test cases

Input is a Kotlin expression that returns the answer, e.g. twoSum(intArrayOf(2, 7, 11, 15), 9)

No custom tests yet. Add one and it runs with “Run tests”.