πŸš€ My Swiggy SDE-2 Round 1 Coding Experience

Ritik GuptaRitik Gupta
3 min read

Recently, I appeared for the SDE-2 Round 1 interview at Swiggy, and I wanted to share my experience to help others who might be preparing for the same.

🧠 Round 1: Coding Interview

The round started with a quick self-introduction followed by two problem-solving questions. It was focused more on problem-solving skills, optimization, and clarity of thought.


βœ… Question 1: Two City Scheduling

πŸ” Problem Summary:

You are given the costs of flying people to two cities. You need to divide them such that exactly N people go to city A and N to city B with the minimum total cost.

πŸ”’ Sample Input:

int[][] costs = {
    {259, 770}, {448, 54}, {926, 667},
    {184, 139}, {840, 118}, {577, 469}
};

πŸ’‘ Approach:

We sort the array based on the cost difference of flying to city A vs. city B ((a[0] - a[1])). This way, the people who are cheaper for city A will come first.

βœ… Code:

class Solution {
    public int twoCitySchedCost(int[][] costs) {
        Arrays.sort(costs, (a, b) -> (a[0] - a[1]) - (b[0] - b[1]));
        int n = costs.length / 2;
        int minCost = 0;
        for (int i = 0; i < n; i++) {
            minCost += costs[i][0];      // First N to city A
            minCost += costs[i + n][1];  // Remaining N to city B
        }
        return minCost;
    }
}

⏱ Time Complexity:

  • O(n log n) for sorting.

πŸ“¦ Space Complexity:

  • O(1), as we sort in-place and don’t use extra space.

βœ… Question 2: Path with Maximum Gold

πŸ” Problem Summary:

You are given a grid where each cell contains some amount of gold. From any cell containing gold, you can move in 4 directions (up/down/left/right) to collect more gold, but you can't visit a cell more than once. Find the maximum gold you can collect.

πŸ’‘ Approach:

This was a classic backtracking problem. We start from every cell with non-zero gold and try all possible paths recursively, keeping track of the maximum gold collected.

βœ… Code:

class Solution {
    private int[][] directions = {{0,1}, {0,-1}, {1,0}, {-1,0}};
    private int maxGold = 0;

    public int getMaximumGold(int[][] grid) {
        int rows = grid.length, cols = grid[0].length;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] > 0) {
                    backTrack(grid, i, j, 0);
                }
            }
        }
        return maxGold;
    }

    private void backTrack(int[][] grid, int row, int col, int currentGold) {
        currentGold += grid[row][col];
        maxGold = Math.max(maxGold, currentGold);

        int originalGold = grid[row][col];
        grid[row][col] = 0;  // mark visited

        for (int[] dir : directions) {
            int newRow = row + dir[0], newCol = col + dir[1];
            if (newRow >= 0 && newRow < grid.length &&
                newCol >= 0 && newCol < grid[0].length &&
                grid[newRow][newCol] > 0) {
                backTrack(grid, newRow, newCol, currentGold);
            }
        }

        grid[row][col] = originalGold;  // backtrack
    }
}

⏱ Time Complexity:

  • O(m * n * 4^k)where k is the number of non-zero gold cells.

πŸ“¦ Space Complexity:

  • O(k) for recursion stack and visited state.

🎯 Conclusion

Both questions tested my ability to:

  • Think through the problem clearly

  • Optimize the approach

  • Write clean and efficient code

The round was well-balanced and gave me a good insight into what Swiggy values in their engineers β€” clarity, efficiency, and structured thinking.

If you're preparing for similar roles, I highly recommend practicing:

  • Sorting-based greedy problems

  • Backtracking and DFS on grids

  • Writing clean and optimized code


0
Subscribe to my newsletter

Read articles from Ritik Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritik Gupta
Ritik Gupta

Hi there! πŸ‘‹ I'm Ritik Gupta, a passionate tech enthusiast and lifelong learner dedicated to exploring the vast world of technology. From untangling complex data structures and designing robust system architectures to navigating the dynamic landscape of DevOps, I aim to make challenging concepts easy to understand. With hands-on experience in building scalable solutions and optimizing workflows, I share insights from my journey through coding, problem-solving, and system design. Whether you're here for interview tips, tutorials, or my take on real-world tech challenges, you're in the right place! When I’m not blogging or coding, you can find me contributing to open-source projects, exploring new tools, or sipping on a good cup of coffee β˜•. Let’s learn, grow, and innovate together!