Expedia Group interview question

It was a hard Hackerrank algorithm question