Getting "Time Limit Exceeded" while running Recursive code
This often confuses people. A friend of mine recently told me:
"I ran my backtracking solution on Leetcode. It said - Time Limit Exceeded. It looks like backtracking is too slow, I should find another way to solve this problem."
What?! Are you serious? Do you know how Leetcode's "Time Limit Exceeded" was probably decided?
At some point, Leetcode engineers probably thought - we can't keep code running forever on our servers, it would increase our server costs. Let's set a time limit.
And that's it. You are judging your solution based on a limit they set - so that their servers can avoid costs.
To be fair, it's not Leetcode's fault. If I was in their place, I would probably do the same. I wouldn't want users running code forever. But you, as the user, shouldn't consider this a failure. Specially if you're preparing for interviews.
In an interview, if your backtracking solution takes a while, that's ok. Many recursive solutions do. Specially with larger input.
Interviewers know this, and they also know that most companies don't use backtracking in their actual work. Specially not on random input sizes. So they won't care if your backtracking solution is exceeding a fixed time limit! They might care about the time and space complexity, but that's it!
This is yet another reason I recommend not running code on Leetcode. Just run it on an IDE and practice verifying the code yourself. Create your own test cases. It's much closer to an actual interview scenario.