DocsApp interview question

Hacker earth two algorithms question