Skip to content

Instantly share code, notes, and snippets.

@akuholla
Last active June 14, 2021 16:37
Show Gist options
  • Save akuholla/499100d43b139f31ca2bbdc852d4dac1 to your computer and use it in GitHub Desktop.
Save akuholla/499100d43b139f31ca2bbdc852d4dac1 to your computer and use it in GitHub Desktop.
NetworkWithLevels.kt
/*
* Product Requirement: The function SlowNetwork::getCountries returns a list of countries. Display the name of the first country on screen.
* Tapping on the country will display a toast saying "The capital for <country name> is <country capital>"
*
* Things to note:
* + Start by creating a new project on Android Studio. Copy the entire contents of this file to your project.
* + You are not allowed to edit the contents of this file.
* + You are free to choose your path for implementing this solution.
* + You are allowed to look through any documentation and/or use a search engine for reference on syntax.
* + Please think out loud to keep the interviewers engaged in your solution.
*/
import android.os.Looper
import java.lang.Exception
enum class LEVEL {
ONE, TWO, THREE
}
val currentLevel = LEVEL.TWO
val COUNTRIES = """
[
{
"country_name": "Afghanistan",
"country_code": "AFG"
},
{
"country_name": "Denmark",
"country_code": "DEN"
},
{
"country_name": "Ireland",
"country_code": "IRL"
},
{
"country_name": "Russian Federation",
"country_code": "RUSS"
},
{
"country_name": "Taiwan",
"country_code": "TAI"
}
]
""".trimIndent()
val CAPITALS = listOf(
"""{ "country_name": "Afghanistan", "capital": "Kabul" }""",
"""{ "country_name": "Denmark", "capital": "Copenhagen" }""",
"""{ "country_name": "Ireland", "capital": "Dublin" }""",
"""{ "country_name": "Russian Federation", "capital": "Moscow" }""",
"""{ "country_name": "Taiwan Federation", "capital": "Taipei" }"""
)
class NetworkWithLevels {
fun getCountries(): String {
if (currentLevel == LEVEL.ONE) {
return "Afghanistan"
} else if (currentLevel == LEVEL.TWO) {
return COUNTRIES
} else if (currentLevel == LEVEL.THREE) {
Thread.sleep(5000)
return COUNTRIES
} else
if (Thread.currentThread().equals(Looper.getMainLooper().thread)) {
throw Exception("Cannot call from main thread")
}
Thread.sleep(5000)
return COUNTRIES
}
fun getCountryCodeFor(countryName: String): String {
if (currentLevel != LEVEL.ONE) {
throw Exception("This function is only for Level one")
}
return "AFG"
}
fun getCapitolFor(countryCode: String): String {
if (currentLevel == LEVEL.ONE) {
if (countryCode.equals("AFG")) {
return "Kabul"
} else {
return "Country code not found"
}
} else {
val array = ArrayList<String>()
if (Thread.currentThread().equals(Looper.getMainLooper().thread)) {
throw Exception("Cannot call from main thread")
}
Thread.sleep(4000)
return when (countryCode) {
"AFG" -> {
CAPITALS.get(0)
}
"DEN" -> {
CAPITALS.get(1)
}
"IRL" -> {
CAPITALS.get(2)
}
"RUSS" -> {
CAPITALS.get(3)
}
"TAI" -> {
CAPITALS.get(4)
}
else -> {
"Unknown code"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment