When Sonja Khan started college, she’d never thought of studying computer science. But when she heard from friends that the intro class was good, she decided to give it a try — and then ended up majoring in it.

Four years later, she has just graduated with a computer science degree, is pursuing a master’s degree and is headed to a summer internship at Facebook.

“I didn’t even know anything about the field before; I had never considered it,” she said. “I signed up for it pretty much on a whim and really enjoyed it.”

Ms. Khan’s story reads like a dream for universities and technology companies — where only about 15 percent of computer science graduates and technical workers are women. The industry has been under pressure to recruit more. The difficult question, though, is how to encourage more women on paths like Ms. Khan’s.