Created
October 15, 2023 19:44
-
-
Save pkafel/86470ca581350014bb89033fbffb9bb1 to your computer and use it in GitHub Desktop.
Solution to Stripe phone interview questions (you can find them here - https://leetcode.com/discuss/interview-question/2585038/Stripe-or-Phone-Screen-or-Senior-SE-or-Reject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OpeningTime { | |
internal var isClosed = false | |
internal val customerPresence: StringBuilder = StringBuilder() | |
} | |
fun computePenalty(log: String, closingTime: Int): Int { | |
val logArray = log.split(" ") | |
var penalty = 0 | |
for (i in logArray.indices) { | |
if (i < closingTime) { | |
if (logArray[i] == "N") penalty++ | |
} else { | |
if (logArray[i] == "Y") penalty++ | |
} | |
} | |
return penalty | |
} | |
fun findBestClosingTime(log: String): Int { | |
var smallestPenalty = Int.MAX_VALUE | |
var bestClosingTime = 0 | |
val logArray = log.split(" ") | |
for (i in 0..logArray.size) { | |
val penalty = computePenalty(log, i) | |
if (penalty < smallestPenalty) { | |
smallestPenalty = penalty | |
bestClosingTime = i | |
} | |
} | |
return bestClosingTime | |
} | |
fun getAllClosingTimesOrderByBegin(log: String): List<Int> { | |
val logParts: List<String> = log.split(" ") | |
var indexOfCurrentPart = -1 | |
val correctOpeningTimes: MutableList<OpeningTime> = ArrayList() | |
for (part in logParts) { | |
when (part) { | |
"BEGIN" -> { // when BEGIN create new opening time and update index to point to current window | |
correctOpeningTimes.add(OpeningTime()) | |
indexOfCurrentPart = correctOpeningTimes.size - 1 | |
} | |
"END" -> { // when END mark the current window as closed and find last not closed window | |
correctOpeningTimes[indexOfCurrentPart].isClosed = true | |
indexOfCurrentPart = correctOpeningTimes.indexOfLast { !it.isClosed } | |
} | |
else -> { // when part of opening time just add it to current window | |
correctOpeningTimes[indexOfCurrentPart].customerPresence.append(part).append(" ") | |
} | |
} | |
} | |
return correctOpeningTimes.map { findBestClosingTime(it.customerPresence.toString().trimEnd()) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment