Shariq Hashme squints at his laptop screen as he scrolls through hundreds of lines of computer code. “I can’t even make sense of it right now,” he says with a grimace. The long string of numbers, symbols and letters would usually be intelligible to Mr. Hashme, a 21-year-old computer science major at the University of Maryland, College Park, but at this moment, he’s having trouble even keeping his eyes open. In the last 27 hours, he has slept just two.

It’s 2:37 a.m. on a Sunday, and he is toiling alongside 671 young software engineers who are camped out in and around a 6,000-square-foot ballroom in Stanford’s alumni center. The space resembles an oversize dorm room during final exams: Temporary workstations are cluttered with computers, electronics cables, half-eaten doughnuts and empty cans of Red Bull. As hundreds of students type feverishly at their laptops, dozens more are passed out in sleeping bags. Meanwhile, an overhead sound system blasts bass-heavy songs — think Kanye West.

“What’s really cool about this atmosphere is that it’s pretty easy to say ‘Screw it’ when it comes to schoolwork,” says Vikram Rajagopalan, a sophomore from the University of Michigan. “Pulling out a textbook is very frowned upon.”

None of these sleep-deprived students is cramming for midterms or racing to finish a class assignment. They’re participating in TreeHacks, a 36-hour contest to program mobile apps, websites or hardware, including aerial drones and virtual reality headsets. The goal of a hackathon, a portmanteau of marathon and hacking, isn’t to obtain confidential data the way hackers infiltrated Sony Pictures last year. Instead, teams attempt to build a new piece of tech, either of their choosing or with code provided by one of the sponsors. At the end, the judges walk from table to table as the programmers show off their projects, just like a school science fair.