Created
November 30, 2022 13:59
-
-
Save vickycj/b2a9f81332781153414f50f9c0c8e00e to your computer and use it in GitHub Desktop.
Video Time Interval Calculation
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
/** | |
* Title : Get Video Watch-Time from Watch Intervals | |
* | |
* Given a list of Video Intervals a user has watched for a video, Find the total watch time of the Video for the User | |
* | |
* for example : if the given List of Intervals are - (0,10), (20,30), (20,40), (15,30) | |
* | |
* then we should get a WatchTime of 30 and actual Intervals are (0,10) and (20,40) | |
* | |
Output Should be like : | |
VideoInterval(startTimeInMills=0, endTimeInMills=10) | |
VideoInterval(startTimeInMills=20, endTimeInMills=40) | |
watchTime = 30 | |
*/ | |
//(10,20), (30, 40), (0,20) | |
fun main() { | |
val videoIntervals : List<VideoInterval> = listOf(VideoInterval(10,20),VideoInterval(30,40),VideoInterval(0,30)) | |
val newList = videoIntervals.sortedWith(compareBy<VideoInterval>{ | |
it.startTimeInMills | |
}) | |
println(computeTotalTime(newList).toString()) | |
} | |
fun computeTotalTime(list: List<VideoInterval>) : Int { | |
var totalTime = 0 | |
var startIndex = 0 | |
var endIndex = 0 | |
var compute = false | |
list.forEach { | |
compute = false | |
if(totalTime == 0) { | |
startIndex = it.startTimeInMills | |
endIndex = it.endTimeInMills | |
compute = true | |
} else { | |
if(it.startTimeInMills >= endIndex) { | |
startIndex = it.startTimeInMills | |
endIndex = it.endTimeInMills | |
compute = true | |
} else if(it.endTimeInMills >= endIndex) { | |
startIndex = endIndex | |
endIndex = it.endTimeInMills | |
compute = true | |
} else { | |
list.remove(this) | |
} | |
///it.startTimeInMills >= endIndex || it.endTimeInMills >= endIndex | |
} | |
if(compute) { | |
totalTime += endIndex - startIndex | |
} | |
} | |
return totalTime | |
} | |
data class VideoInterval(val startTimeInMills : Int, val endTimeInMills : Int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment