10 Crazy Lord Of The Rings Theories And Why They Might Be True
2. Gandalf Always Knew The Riddle In Moria
When Saruman cut off the Fellowship's path over Caradhras, they had two options. Either make for the Gap of Rohan, or cut under the mountain through the Mines of Moria. The choice fell to Frodo who decided on Moria, not knowing the flame and shadow that awaited them there.
It is clear that Gandalf was already aware of the Balrog at this point, but he went with Frodo's word all the same. This could be because he trusted his Hobbit ringbearer, and was confident that they could make it through the Mines alive, or perhaps he never quite intended to go through them at all.
At the door to Khazad-Dûm, Gandalf reads the Elvish inscription to himself, then aloud in plain English. To have spoken "speak friend and enter" in Elvish would have opened the door, but he chose not to. Maybe Gandalf, foreseeing his own doom at the hands of the Balrog, played the fool to convince the company of another path.
It seems unlikely that Frodo could figure this riddle out and not Gandalf, only the wise wizard didn't count on the Hobbit being as smart as he was brave. He said it himself, Hobbits can always surprise you, probably a bittersweet moment before entering the dark cave knowing he would die there.