Home › Company-wise questions › L&T Mindtree › Permutations ← L&T Mindtree bank
L&T Mindtree · Question 38 · Backtracking
Permutations Backtracking Java code ⏱ O(n!*n) 💾 O(n)
Service-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem You are given an array of distinct integers. Return all possible permutations.
Approach Swap each position with every later choice, recursing and swapping back.
Time O(n!*n)Space O(n)
How it works 1 Fix each index with every remaining choice. 2 Swap back to restore state. Sample input / output Input:
nums = [0, 1]
Output:
[[0, 1], [1, 0]] Constraints Distinct values; 1 <= length <= 6.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n!*n)Space O(n)Loops 2Branches 1Returns 2source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
yes no ↻ repeat START import java.util.* DEFINE class Main DEFINE static List<List<Integer>> permute(int[] nums) List<List<Integer>> result = new ArrayList<>() backtrack(nums, 0, result) RETURN result DEFINE static void backtrack(int[] nums, int start, List<List<Integer>… if (start == nums.length) List<Integer> cur = new ArrayList<>() for (int v : nums) cur.add(v) result.add(cur) RETURN for (int i = start; i < nums.length; i++) swap(nums, start, i) backtrack(nums, start + 1, result) swap(nums, start, i) DEFINE static void swap(int[] a, int i, int j) DEFINE t = a[i] a[i] = a[j] a[j] = t DEFINE static void main(String[] args) System.out.println(permute(new int[]{0, 1})) END start / end statement condition loop returns definition← Q37 · Letter Combinations of Phone Number Q39 · Subsets → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →