Legal in terms of statutes on the books in most jurisdictions? Yes.
But likely disallowed by the terms used in at least one company's employment contract (if not both). Breaching the terms of an employment contract is not necessarily "illegal", in the sense that it's not considered a criminal act and will not attract criminal penalties.
However, there may be civil consequences for breaching; ranging from termination from one or both companies, being hit with an injunction demanding you cease working at one company or the other, or in extreme cases, perhaps being held liable for damages (and legal costs!) if one company believes (and can demonstrate) that you've caused them economic harm by assisting their competitor (for instance, by taking internal technical knowledge from Google to Amazon) when in your contract you promised to do no such thing.
And note that there's room for nuance. For instance, if an employer agrees to pay you a salary of $100,000/year and instead only pays you $1000/year, that would be both a civil breach and (in most locales) a criminal act as compensation of $1000/year would run afoul of minimum wage laws. So there are cases where a breach can also be illegal.
But if we disregard practical concerns and assume that you could get an employment contract that doesn't restrict your right to work for a competing company at the same time, then yes, it would be be legally valid and not risking any civil penalties to work for both companies concurrently. But the odds of you getting such a contract are extremely low.