FORTUNE -- On a Friday morning in late August, six weeks into her new job as CEO of Yahoo (YHOO), Marissa Mayer was brainstorming about how to make the company innovative again. She became particularly animated when she talked about empowering employees and ending corporate gridlock, recalls Patricia Moll Kriese, one of several former Google colleagues who followed Mayer to the struggling web giant. "We're talking about streamlining process, reducing bureaucracy and removing jams," Mayer told Kriese with a sly smile. At that moment the acronym-loving CEO stumbled upon the name for a new companywide initiative. "Process, bureaucracy, jams," she said. "We'll call it PB&J!"
That afternoon Mayer hosted one of her FYI meetings, the Friday town halls that she started holding for employees soon after she arrived from Google (GOOG) in July. It wasn't lost on Mayer that PB&J was sure to remind people of the Peanut Butter Manifesto, a 2006 rant by a Yahoo manager about how the company had spread itself too thin. Mayer explained her PB&J -- a bunch of small changes to make work more productive -- and urged employees to suggest more changes: Employees log in to an internal website and vote on one another's ideas. The site ranks the ideas by popularity; management implements the best ones. PB&J is Google-like and quintessentially Mayer -- data driven, democratic, and fun. That evening, in an e-mail to 12,500 Yahoo employees, Mayer declared her plan to make Yahoo "the absolute best place to work."
MORE: 40 Under 40 - the list
That's a pretty ambitious goal for a first-time CEO walking into a company with flat sales, an ever-shrinking workforce, and a revolving door at the top: Yahoo has had four CEOs in five years. Though it has a vast user base of 700 million consumers, 18-year-old Yahoo has utterly failed to produce the must-use products in search, social, and mobile that it needs to keep pace with Google and Facebook (FB). The company that once boasted a market value of $125 billion (to be sure, it peaked during the dotcom bubble) is now valued at about $20 billion, and the stock has scarcely budged in the past four years. Why should investors -- or employees, for that matter -- believe that finally Yahoo has a boss who can fix the culture and attract the talent to upgrade innovation? "I really wish her well," says former CEO Carol Bartz, whom the board fired last year. "Changing culture is not a sprint," she adds. "It's a marathon. It's very, very hard to affect culture."
Logic and Yahoo's longtime troubles could lead anyone to believe that yet another CEO will fail. But Mayer is different -- really different. Obviously there's her youth. At 37, she is the youngest CEO -- male or female -- of a Fortune 500 company, a distinction that lands her at No. 3 on Fortune's annual 40 Under 40 ranking. She was an unexpected pick to run Yahoo, then surprised the business world when she revealed, a few hours after Yahoo named her CEO, that she was pregnant. (Fortune got the scoop.) Mayer delivered a baby boy on Sept. 30, fully intending to return to work in a couple of weeks.
If anyone can manage the high-wire act of turning around a company and caring for a newborn, it is Mayer. She is incredibly energetic and loves hard work -- she pulled an all-nighter at least once a week in Google's early days. (She's also rich, so there's no reason to believe she won't have supremely reliable childcare.) While her limited P&L experience gave the Yahoo board pause, her drive to succeed won the directors over. Michael J. Wolf, the former MTV president and Yahoo director who drove the CEO search, says that the board wanted someone "who believed Yahoo can grow again" and, crucially, "who believed they could do it."
Mayer brings with her a passion for products, a knack for talent development, and bona fide tech cred, all of which Yahoo has lacked for too long. Mayer is a Stanford grad, with a master's degree in computer science. Google's 20th employee and first female engineer, she spent five years heading search products and "user experience" -- shaping the way people navigate the Internet while protecting the elegant simplicity of Google's websites. In 2010 she was put in charge of a portfolio that included local services, maps, and location services -- a lateral move that some deemed a demotion because it yanked her from Google's all-important search business. Mayer, also removed from Google's important operating committee when Larry Page took over as CEO from Eric Schmidt, proved her resilience and strengthened her new unit by engineering an acquisition of Zagat, a purveyor of restaurant guides. She also has insight into how huge companies work, having joined the board of Wal-Mart (WMT) early this year.
Mayer had no plans to leave Google. But when Spencer Stuart recruiter Jim Citrin called last June, she agreed to meet with the directors. She told them that Yahoo's products need to become "more innovative and delightful." The board promised her free rein to shake up the organization and time to prove herself. In less than a month she was Yahoo's new chief.
Three months into the new regime, Mayer is remaking Yahoo in her image -- and Google's too. First came free food in the cafeteria. Then free smartphones for all employees -- iPhones or devices running Microsoft's Windows or Google's Android operating systems. (No BlackBerrys.)
Mayer has told employees that she intends to be "radically transparent" with them, but for now her broader vision for Yahoo remains under wraps. Will she focus on bolstering Yahoo's content offerings to attract new users? Will she invest in new tech tools to help advertisers expand their reach? Will she partner with other tech companies -- including former employer Google?
Mayer declined to speak with Fortune for this article, but we interviewed dozens of friends and people who have worked with Mayer -- including some executives who followed her to Yahoo. And while her strategic direction for Yahoo is still a work in progress, the energy at the company is already changing. It is becoming more social, more agile, and more ambitious. Come to think of it, more like her.
"By my nature I'm a very shy person," Mayer told me when I visited her on Google's Silicon Valley campus in 2008. If you've ever watched the You-Tube video of Mayer interviewing Lady Gaga at Google Goes Gaga, or if you saw Vogue's take on her wedding to Zachary Bogue in 2009, you would not imagine Mayer to be an introvert. But growing up in Wausau, Wis. -- "Norman Rockwell America," she calls it -- she was the girl who knew all the answers in class and waited to be called on.
Mayer (pronounced MY-er) grew up with a hockey-loving brother, Mason, four years younger than her, a civil-engineer dad who designed water-treatment plants, and a mom who taught art to kids. Margaret Mayer instilled in her daughter not only a love for design and fashion but also a willingness to try anything. Every day of the week she took Marissa to a different activity or lesson -- swimming, skating, ballet, piano, cake decorating. In high school Marissa was an all-round overachiever: captain of the debate team and the pom-pom squad, which performed energetic dance numbers at halftime during football and basketball games.
"When Marissa became captain of the pompom squad, she wasn't in with that clique of girls, but she won them over in three ways," recalls Abigail Garvey Wilson, Mayer's best friend from childhood. "First, sheer talent. Marissa could choreograph a great routine. Second, hard work. She scheduled practices lasting hours to make sure everyone was synchronized. And third, fairness. With Marissa in charge, the best dancers made the team."
Even back in her teenage years, Mayer was mathematical, analytical, and, to use her term, "matrix-driven." When she got accepted at every college she applied to -- Harvard, Yale, Northwestern, Duke, and six others -- she created a spreadsheet with student-teacher ratios, median SAT scores, and various measures of campus life to help her figure out where to go. She chose Stanford partly because, for a Wisconsin girl, she said, "it was unique." (Coincidentally, Mayer's predecessor Carol Bartz also grew up in Wisconsin, surely making Yahoo the only Fortune 500 company to have hired two Wisconsin-raised women as CEOs.)
She arrived intending to become a pediatric neurosurgeon, but she shifted to symbolic systems, an eclectic course of study. SymSys majors study psychology, linguistics, philosophy, and computer science to figure out how people learn and reason, and how to endow computers with humanlike behavior. It was a perfect path of study for Mayer, who, while shy, isn't a loner. "We were all intense students, but Marissa stood out because she had unusual balance and a deep understanding of people and how to relate to them," says Hosain Rahman, a freshman dorm-mate of Mayer's who is now CEO of mobile-device maker Jawbone and No. 24 on Fortune's 40 under 40 list.
Mayer didn't plan to stay in Silicon Valley after she got her master's in computer science in 1999. But when one of her professors, Eric Roberts, e-mailed her to let her know about two guys, Sergey Brin and Larry Page, who were starting a company to organize the web, she decided to go for a job interview. "Sergey drilled me [about data analysis techniques]. Larry said almost nothing, to the point that I almost asked him, 'Do you speak?' " She and Page later dated before they both married other people.
Mayer drew up another spreadsheet to assess her 14 job offers, which included Oracle (ORCL), Toyota (TM), Carnegie Mellon, McKinsey, and Google. It was down to the last two when Mayer thought to herself, "I could give advice to Fortune 500 companies, or I could help change the world." She chose Google, although, she admitted, "I gave these guys a 2% chance."
During her first two years at Google, Mayer worked 100 hours a week. From the day she wrote her first screensaver, she loved programming. Moving into management meant giving that up day to day. "It was hard because it took me out of my comfort zone." That idea of trying uncomfortable things became part of her personal career credo: "Do things that you're not quite ready to do. And surround yourself with the smartest people."
Superhuman stamina, high standards, and a willingness to do any job herself made Mayer a challenging person to work for. "Her weakness was an unwillingness to delegate," says Craig Silverstein, who joined Google before Mayer and left to develop software at Kahn Academy early this year. "She doesn't need any sleep," he explains. "When you have four or five more hours in the day than most people do, you don't learn to delegate because you don't need to." She was sometimes curt and controlling, but as her product management organization grew, Mayer had to delegate to succeed.
Mayer became a motivational leader, in part because she recognized that leadership depends on nurturing talent. In 2002 she started the APM -- associate product manager -- program to teach high-potential engineers how to be leaders. The APMs got assignments, meetings with Google's top brass, and lots of attention from Mayer, who took each class on a weeklong trip abroad. Jess Lee (No. 32 on the 40 Under 40), who was a Google APM in 2004 after earning a computer- science degree at Stanford, remembers a whirlwind week with Mayer and 11 fellow APM grads in Tokyo, Bangalore, and Zurich, where they visited Google's local offices and met key customers. "The philosophy was to force us to operate at a higher level," says Lee, who is now CEO of fashion startup Polyvore.
Google's APM program has become a farm system for tech talent across Silicon Valley -- as well as a vital network of people whom Mayer may tap to help her turn around Yahoo. Indeed, the program, along with Mayer's yen for organizing extracurricular activities such as movie nights for employees, caught the eye of the Yahoo board. "Here's someone who engendered a tremendous amount of loyalty," says Wolf, who joined the board in May as part of a slate that activist investor Dan Loeb, now Yahoo's biggest shareholder, brought in. "People want to work for her."
Following the second part of her career credo, to "work with the smartest people," Mayer has moved quickly to recast Yahoo's senior ranks. To be chief marketing officer, she hired Kathy Savitt, an alum of Amazon.com (AMZN) who went on to build her own startup, Lockerz. Mayer replaced CFO Tim Morse, who focused on cost cutting, with Ken Goldman, a growth-oriented former finance chief of computer security company Fortinet. For a new position called EVP of people and development, Mayer recruited a veteran investment banker named Jackie Reses, who worked at Goldman Sachs (GS) and then led the U.S. media practice at private equity firm Apax Partners. Reses is Yahoo's dealmaker-plus, overseeing HR, business development, strategy, and M&A.
Though she's added strategic firepower to her team, board members don't expect Mayer to go on a big-company buying spree. According to Wolf, Mayer is well aware that Yahoo has spent more than $6 billion on acquisitions in the past 10 years and today has little to show for it. The lousy acquisition record is one reason that Mayer, after collecting $4.3 billion last month from the sale of half of Yahoo's stake in Chinese e-commerce giant Alibaba, opted to distribute 85% of the proceeds to shareholders in the form of stock buybacks and dividends. Yahoo has plenty of cash at its disposal; Goldman Sachs analyst Heath Terry forecasts that it will have a stockpile of $6.9 billion at the end of the year. But the board hired Mayer to build, not buy, and she is hardly the type to spend $1 billion on a company like Instagram, the photo-sharing site that Facebook recently purchased.
Instead, Mayer has told employees that she will follow "the rule of 100 million": Invest only in products and ventures that have a good shot at reaching 100 million users and $100 million in revenue. She has said she is keen on " acqui-hires" -- Silicon Valley lingo for buying startups to get great talent. (It shouldn't be hard for Mayer to tap into the startup scene: She's an active angel investor who has put seed money into ventures such as Square, Minted, and Uber.) Mayer is believed to be particularly impressed with acquisitions such as Yahoo's 2011 purchase of IntoNow, a company that lets viewers "tag" and share content in TV shows. Yahoo bought the startup for about $30 million, just as it was on the cusp of a growth surge.
She'll likely target nimble ad-technology firms such as PubMatic and Criteo that can help Yahoo optimize the value of its sites to advertisers and bring in more revenue. Mobile technology, where Yahoo hasn't invested enough, is also a priority. Popular mobile news-reading apps like Pulse and Flipboard are the kinds of acquisitions that may lure Mayer.
MORE: 40 Under 40: Visualizer
The new CEO is also on the hook to address Yahoo's declining share in search, which brings in about a third of Yahoo's revenue. Bartz and Microsoft CEO Steve Ballmer forged a 10-year partnership that makes Microsoft (MSFT) the backend manager of Yahoo's search operation while Yahoo handles the front-end presentation: what you get when you search via Yahoo or Bing. While the partnership has been strained, Mayer told the board that it was smart to outsource the costly infrastructure management. In fact, Mayer likens Yahoo to a vintner who sells off his vineyard and can still produce great wine with someone else's grapes. Yahoo needs better winemakers, pure and simple. Sophisticated product engineers would personalize Bing's and Yahoo's search results for its users, making the experience more, well, Googley. Mayer's ability to do a search deal with Google is limited by antitrust law, though Mayer's old boss Eric Schmidt has suggested publicly that he would be open to a partnership.
It is a long to-do list, plus she'll eventually have to address questions about what she wants Yahoo to be when it cleans up its act. And then there's her home-work balancing act. Mayer's decision to return to work so soon after her delivery has invited commentary and criticism from women and men who wonder if she's setting an unhealthy precedent for working mothers. But Mayer isn't a baby boomer fighting for her rights in the workplace; she's a member of the under-40 set -- a generation marked by its mobility, flexibility, and independence. Returning to work on her own terms is very much in keeping with her cohort.
If nothing else, her new gig will take her out of her comfort zone. If history is any guide, that's exactly the kind of thing that Mayer thrives on.
This story is from the October 29, 2012 issue of Fortune.
Ever since Fortune, in 1998, started ranking the top women in business (yes, we were first), I've been asking the stars of the Most Powerful Women list how they reached the top and how they stay there. One month away from revealing our 2011 MPW rankings, now seems a good time to share some of their best career tips. Here is my Top 10:
1. Don't plan your career. Most of MOREPatricia Sellers - Aug 30, 2011 10:48 AM ET
|Homeless college students seek shelter during breaks|
|Five things you didn't know about Bernie Madoff's epic scam|
|Budget deal hits federal workers|
|Snowden docs had NYTimes exec fearing for his life|
|JPMorgan patents Bitcoin-like payment system|