In my case it seems to be a software problem.
I have read about the same problems with other macs and people have replaced the hard drives with mixed results. As I said earlier, I do not believe every case is hardware failure.
But if you reformatted your drive and installed a fresh copy of OSX with updated patches then it might be a hardware problem but obviously not the hard drive.
In 2007-2008 time frame Apple got a hold of bad NVIDIA graphics cards that acted like this. Apple went 6 months past the warranty in my case to replace it.
I read extensively about the vulnerability known as CVE-2014-6271 or The ShellShock Bash Exploit, and interestingly enough one of the posts mentioned the problem of boot issues.
Here is how to test for the vulnerability and see if you are at risk or have been exploited.
http://mac-how-to.wonderhowto.com/how-to/every-mac-is-vulnerable-shellshock-bash -exploit-heres-patch-os-x-0157606/
I am not sure that was my problem but I was at risk and noticed that the firewall kept turning off as well as permissions changing.
After repairing permissions and updating OS X from Yosemite to El Capitan I do not have the problem anymore and the mac works great!
Supposedly this exploit has been patched in El Capitan but they do give instructions on how to manually do it in older versions of OSX.
Again I am not certain of the problem but hope your outcome is as good as mine.