Facebook Interview Question

Country: United States

Comment hidden because of low score. Click to expand.
of 2 vote

Visit AONECODE.COM for ONE-ON-ONE private lessons by FB, Google and Uber engineers!

SYSTEM DESIGN Courses (highly recommended for candidates of FB, LinkedIn, Amazon, Google and Uber etc.),
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algorithms, Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!


public void nextPermutation(int[] nums) {
        if(nums.length <= 1)  return;

        int i = nums.length - 1;
        while(i >= 1 && nums[i] <= nums[i-1]) i--; //find first number which is smaller than it's after number
        if(i!=0) swap(nums,i-1); //if the number exist,which means that the nums not like{5,4,3,2,1}

    private void swap(int[] a,int i){
        for(int j = a.length - 1;j > i;j--){
            if(a[j] > a[i]){
                int t = a[j];
                a[j] = a[i];
                a[i] = t;

    private void reverse(int[] a,int i){//reverse the number after the number we have found
        int first = i;
        int last = a.length - 1;
            int t = a[first];
            a[first] = a[last];
            a[last] = t;

- jojocat1010 May 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Looks like your method will take int array. But in the above problem it says int number. How does it work ?

- sarunreddy82 May 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

int greater(int num)
	string str = to_string(num);
	int n = str.size();
	int i = 0, j=0;
	for(i=n-1; i>-1; i--)
		for (j=i-1; j>-1; j--)
			if (str[i] > str[j])
				str = str.substr(0, j) + str[i] + str.substr(j, i - j) + str.substr(i+1, n-(i+1));
				sort(str.begin() + j + 1, str.end());
				return stoi(str);
	return -1;

- aman.bansal4u May 18, 2018 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More