Home > Enterprise >  Kotlin: How to utilize Kotlin data structure to write efficient solutions during interview
Kotlin: How to utilize Kotlin data structure to write efficient solutions during interview

Time:01-19

I got this question on an interview I was doing and flopped at the last part with the Kotlin. I am trying to recreate fun taskCompleted() I wanted to see how I can implement it, just to play with it. So this is what I was given

data class Article(val title: String, val content: String, val duration: Int, val date: Int)

data class StreakInfo(val goalsMet: Int, val breakDown: List<Boolean>)

interface ReadingGoalService{
    fun getDailyReadingGoal(): Int
}

interface TaskService{

    fun allTasks(): List<Article>
}

class MockInterview{

   
    fun taskCompleted(){}

    //temporary
    private fun today(): Int{

        return 1
    }
}

fun getAllSteaks(streakInfo: StreakInfo){
    //don't implement
}

The ask was to do fun taskCompleted() where, I was expected to the following

        //TODO 1 current day is today
        //TODO 2 take all the articles that were read on the current day
        //TODO 3 sum readingDurations of articles for current Day
        //TODO 4 check if sum of readingDurations is >= than reading goal
        // TODO 5 if sum of readingDuration is >= goal repeat 2..3.. and 4. for currentDay-1
        //TODO 6 if sum of readingDuration is < goal - stop

Any ideas how I can do this in Kotlin? I plan on using this as a learning process.

CodePudding user response:

As I mentioned, I highly suggest you take a look at the Kotlin Koans since they walk you through many examples that I think would certainly teach you how to perform almost all of those TODO's! (and they have the solutions!)

In this interview were testing whether you knew on how to use the aggregate and mapping functions from the Kotlin Standard Library and checking whether you would see that there was a lot of code that you could re-use by making them functions with the right amount of parameters

Here's how I would approach it:


fun today() = 1 // TODO 1: Current day is today

fun tasksDoneByDay(taskService: TaskService, day: Int = today()): List<Article> {
    // TODO2: Here I have all tasks that were done at "today()"
    return taskService.allTasks().filter { task -> task.date == day }
}

// TODO 3
fun durationsForDay(taskService: TaskService, day: Int = today()) = tasksDoneByDay(taskService, day).sumBy { it.duration }


// TODO 4
fun durationsForDayMeetGoal(taskService: TaskService, goal: Int, day: Int = today()) = durationsForDay(taskService, day) >= goal

// TODO 5 if sum of readingDuration is >= goal repeat 2..3.. and 4. for currentDay-1

fun todo5(taskService: TaskService, goal: Int) {
  val today = today()
  return when(val durationsForTodayMeetGoal = durationsForDayMeetGoal(taskService, goal, today)) {
    false -> durationsForDayMeetGoal(taskService, goal, today - 1)
    true -> durationsForTodayMeetGoal
  }
}

// TODO 6 I don't understand...

essentially the same as todo five but when it's false dont do anything?
  •  Tags:  
  • Related