LogIn
I don't have account.
Sponsored -53%
Zebronics #1 Best Seller

ZEBRONICS Blanc Slim Wireless Mouse — Rechargeable, BT + 2.4GHz (Black)

Up to 1600 DPI · Silent clicks · 63g · Multicolor LED

★★★★★ 4.0 (9,512) 5K+ bought last month
₹376 M.R.P. ₹799Save ₹423
Buy Now
Adℹ

Goldman Sachs Round 2 Coding Interview

Ajeet Pawar
59 Views
Amazon Pay offer
Adℹ

I recently interviewed for the Analyst (SDE) role at Goldman Sachs, Bangalore, sharing question that was asked ininterview are

  • Gas Station

  • Minimum Window Substring

Problem 1: Gas Station

You are given two integer arrays:

  • gas[i]: the amount of gas available at station i.

  • cost[i]: the amount of gas required to travel from station i to (i+1).

You start at any gas station with an empty tank. Your task is to determine:

If there exists a starting gas station index from which you can travel around the circuit once and return to the same station.

If no such starting station exists, return -1.

Example

Input:

gas = [1,2,3,4,5]  
cost = [3,4,5,1,2]

Output:

3

Explanation:

Start at station 3 (0-based index). The gas collected and spent balances out, allowing a full circuit.

Intuition & Explanation

  • First, check the total gas vs. total cost.

  • If sum(gas) < sum(cost), it’s impossible to travel around.

  • Otherwise, we can always find a starting station.

Use a greedy approach:

  • Traverse stations and maintain a running balance (tank).

  • If at any point tank < 0, reset the start position to the next station because the previous segment can not be a valid start.

public class GasStation {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int totalGas = 0, totalCost = 0;
        int tank = 0, startIndex = 0;

        for (int i = 0; i < gas.length; i++) {
            totalGas += gas[i];
            totalCost += cost[i];
            tank += gas[i] - cost[i];
            // If tank becomes negative, reset start index
            if (tank < 0) {
                startIndex = i + 1;
                tank = 0;
            }
        }

        return totalGas >= totalCost ? startIndex : -1;
    }
}

Time Complexity Analysis

  • Time Complexity: O(n) → Single pass through arrays.

  • Space Complexity: O(1) → Constant extra space.

Case Analysis:

  • Best Case: The first station is valid → still O(n) because we must check all.

  • Worst Case: Need to traverse full array → O(n).

  • Impossible Case: When total gas < total cost → detected in a single pass.

Problem 2: Minimum Window Substring

Given two strings s and t, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If no such substring exists, return "".

Example

Input:

s = "ADOBECODEBANC"  
t = "ABC"

Output:

"BANC"

Explanation:

The substring "BANC" is the smallest window in s containing all characters of t.

Intuition & Explanation

  • This is a classic sliding window + hashmap problem.

  • Count frequencies of characters in t.

  • Expand the right pointer (r) until the window contains all characters of t.

  • Once valid, shrink from the left (l) to minimize the window.

  • Keep track of the smallest valid window.

import java.util.*;

public class MinimumWindowSubstring {
    public String minWindow(String s, String t) {
        if (s.length() < t.length()) return "";
        Map<Character, Integer> tMap = new HashMap<>();
        for (char c : t.toCharArray()) {
            tMap.put(c, tMap.getOrDefault(c, 0) + 1);
        }
        Map<Character, Integer> window = new HashMap<>();
        int left = 0, right = 0;
        int required = tMap.size(), formed = 0;
        int minLen = Integer.MAX_VALUE, start = 0;

        while (right < s.length()) {
            char c = s.charAt(right);
            window.put(c, window.getOrDefault(c, 0) + 1);

            if (tMap.containsKey(c) && window.get(c).intValue() == tMap.get(c).intValue()) {
                formed++;
            }

            // Try to shrink window
            while (left <= right && formed == required) {
                if (right - left + 1 < minLen) {
                    minLen = right - left + 1;
                    start = left;
                }

                char leftChar = s.charAt(left);
                window.put(leftChar, window.get(leftChar) - 1);
                if (tMap.containsKey(leftChar) && window.get(leftChar) < tMap.get(leftChar)) {
                    formed--;
                }
                left++;
            }
            right++;
        }

        return minLen == Integer.MAX_VALUE ? "" : s.substring(start, start + minLen);
    }
}

Time Complexity Analysis

  • Time Complexity: O(|s| + |t|)

Each character processed at most twice (once by right pointer, once by left pointer).

  • Space Complexity: O(|t|) for hashmap storage.

Case Analysis:

  • Best Case: t is very small compared to s. Still O(|s|).

  • Worst Case: t has all unique characters → full traversal needed.

  • Edge Case: If s.length() < t.length(), return "" immediately.

Final Thoughts

  • Both Gas Station and Minimum Window Substring are popular DSA interview problems that test your ability to apply greedy strategies and sliding window techniques effectively.

  • Gas Station: Focuses on greedy choice and cumulative balance.

  • Minimum Window Substring: Tests your mastery of sliding window and hashmap optimization.

Mastering these will not only help in interviews but also strengthen your problem-solving and optimization skills.

Trending Developer Reads

Responses (0)

Write a response

CommentHide Comments

No Comments yet.

"},"4":{"id":4,"name":"ZEBRONICS Blanc mouse ad","otherData":"{}","contentFormat":2,"content":"
Sponsored -53%
Zebronics #1 Best Seller

ZEBRONICS Blanc Slim Wireless Mouse — Rechargeable, BT + 2.4GHz (Black)

Up to 1600 DPI · Silent clicks · 63g · Multicolor LED

★★★★★ 4.0 (9,512) 5K+ bought last month
₹376 M.R.P. ₹799Save ₹423
Buy Now
"},"2":{"id":2,"name":"Crousal Ads","otherData":"{}","contentFormat":2,"content":"
Sponsored -34%
iQOO Amazon's Choice

iQOO Z10 Lite 5G — Cyber Green, 4GB RAM, 128GB Storage

Dimensity 6300 · 50MP Sony AI Camera · 6000 mAh · IP64

★★★★★ 4.0 (2,052) 500+ bought last month
₹18,997 M.R.P. ₹28,999Save ₹10,002
Buy Now
"},"1":{"id":1,"name":"Banner Ads","otherData":"{}","contentFormat":2,"content":"\n
\n \n \n \"Amazon\n \n
"}}},"blogDetails":{"isWriter":false,"likes":0,"isLikedByUser":false,"tagIds":[134,177,360,363,364],"tags":[{"id":134,"name":"Interview","maskingName":"interview","isFeatured":true},{"id":177,"name":"DSA","maskingName":"dsa","isFeatured":true},{"id":360,"name":"SDE 1","maskingName":"sde-1","isFeatured":false},{"id":363,"name":"Interview Experience","maskingName":"interview-experience","isFeatured":true},{"id":364,"name":"Goldman Sachs","maskingName":"goldman-sachs","isFeatured":false}],"comments":[],"bookmark":{"isBookmarked":false,"count":0},"author":{"userId":"MjlfNF84LVsyMF0ybi10Q19BTmdlX19JdA==","name":"Ajeet Pawar"},"viewCount":58,"showBannerImage":false,"seoTags":null,"content":"

I recently interviewed for the Analyst (SDE) role at Goldman Sachs,\nBangalore, sharing question that was asked ininterview are

\n\n

Problem 1: Gas Station

\n

You are given two integer arrays:

\n\n

You start at any gas station with an empty tank. Your task is to determine:

\n

If there exists a starting gas station index from which you can travel around the\ncircuit once and return to the same station.

\n

If no such starting station exists, return -1.

\n

Example

\n

Input:

\n
gas = [1,2,3,4,5]  \ncost = [3,4,5,1,2]\n
\n

Output:

\n
3\n
\n

Explanation:

\n

Start at station 3 (0-based index). The gas collected and spent balances out, allowing a full circuit.

\n

Intuition & Explanation

\n\n

Use a greedy approach:

\n\n
public class GasStation {\n    public int canCompleteCircuit(int[] gas, int[] cost) {\n        int totalGas = 0, totalCost = 0;\n        int tank = 0, startIndex = 0;\n\n        for (int i = 0; i < gas.length; i++) {\n            totalGas += gas[i];\n            totalCost += cost[i];\n            tank += gas[i] - cost[i];\n            // If tank becomes negative, reset start index\n            if (tank < 0) {\n                startIndex = i + 1;\n                tank = 0;\n            }\n        }\n\n        return totalGas >= totalCost ? startIndex : -1;\n    }\n}\n
\n

Time Complexity Analysis

\n\n

Case Analysis:

\n\n

Problem 2: Minimum Window Substring

\n

Given two strings s and t, return the minimum window substring of\ns such that every character in t (including duplicates) is included\nin the window.\nIf no such substring exists, return \"\".

\n

Example

\n

Input:

\n
s = \"ADOBECODEBANC\"  \nt = \"ABC\"\n
\n

Output:

\n
\"BANC\"\n
\n

Explanation:

\n

The substring \"BANC\" is the smallest window in s containing all\ncharacters of t.

\n

Intuition & Explanation

\n\n
import java.util.*;\n\npublic class MinimumWindowSubstring {\n    public String minWindow(String s, String t) {\n        if (s.length() < t.length()) return \"\";\n        Map<Character, Integer> tMap = new HashMap<>();\n        for (char c : t.toCharArray()) {\n            tMap.put(c, tMap.getOrDefault(c, 0) + 1);\n        }\n        Map<Character, Integer> window = new HashMap<>();\n        int left = 0, right = 0;\n        int required = tMap.size(), formed = 0;\n        int minLen = Integer.MAX_VALUE, start = 0;\n\n        while (right < s.length()) {\n            char c = s.charAt(right);\n            window.put(c, window.getOrDefault(c, 0) + 1);\n\n            if (tMap.containsKey(c) && window.get(c).intValue() == tMap.get(c).intValue()) {\n                formed++;\n            }\n\n            // Try to shrink window\n            while (left <= right && formed == required) {\n                if (right - left + 1 < minLen) {\n                    minLen = right - left + 1;\n                    start = left;\n                }\n\n                char leftChar = s.charAt(left);\n                window.put(leftChar, window.get(leftChar) - 1);\n                if (tMap.containsKey(leftChar) && window.get(leftChar) < tMap.get(leftChar)) {\n                    formed--;\n                }\n                left++;\n            }\n            right++;\n        }\n\n        return minLen == Integer.MAX_VALUE ? \"\" : s.substring(start, start + minLen);\n    }\n}\n
\n

Time Complexity Analysis

\n\n

Each character processed at most twice (once by right pointer, once by left pointer).

\n\n

Case Analysis:

\n\n

Final Thoughts

\n\n

Mastering these will not only help in interviews but also\nstrengthen your problem-solving and optimization skills.

","categoryId":363,"subCategoryId":364,"contentFormat":5,"blogId":9,"userId":"MjlfNF84LVsyMF0ybi10Q19BTmdlX19JdA==","title":"Goldman Sachs Round 2 Coding Interview","url":"goldman-sachs-round-2-coding-interview","bannerImage":"","seoDescription":"","generatedOn":"2025-09-08T17:17:21","updatedOn":"2026-08-30T07:06:26"},"popularContents":[{"id":135,"title":"Spinny SDE-1 Interview Experience (Selected)","url":"spinny-sde-1-interview-experience-selected","description":null,"seoDescription":"Spinny SDE-1 Interview Experience (Selected) | 4 Rounds Breakdown: Coding, LLD, Java, SQL + Real Interview Questions","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051724Z","viewCount":0},{"id":173,"title":"Uber SDE-2 Interview Experience : LLD, HLD, DSA & Hiring Manager Round","url":"uber-sde-2-interview-experience-lld-hld-dsa-hiring-manager-round","description":null,"seoDescription":"Read a real Uber SDE-2 interview experience covering DSA, low-level design, high-level system design, behavioral rounds, hiring freeze and key takeaways.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051831Z","viewCount":0},{"id":163,"title":"Top 50 SQL Interview Questions and Answers (Beginner to Intermediate)","url":"top-sql-interview-questions-and-answers-beginner-to-intermediate","description":null,"seoDescription":"Top 50 SQL Interview Questions and Answers (Beginner to Intermediate). Top SQL Interview Questions for fresher. Frequently asked SQL interview questions with answers","contentType":2,"generatedOn":"2026-10-14T22:22:16.6044166Z","viewCount":0},{"id":138,"title":"DSA Patterns for Coding Interviews: Complete LeetCode Roadmap (Beginner to Advanced)","url":"dsa-patterns-for-coding-interviews-complete-leetcode-roadmap-beginner-to-advanced","description":null,"seoDescription":"Master every LeetCode pattern for coding interviews. Learn Arrays, Sliding Window, Graphs, Dynamic Programming, Trees, Heaps and more with 250+ curated problem","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051739Z","viewCount":0},{"id":130,"title":"Rippling SDE-2 Interview Experience (4+ Years Experience) | Offer Received | Coding, LLD & System Design","url":"rippling-sde-2-interview-experience-4-years-experience-offer-received-coding-lld-system-design","description":null,"seoDescription":"Detailed Rippling SDE-2 interview experience with complete 5 rounds, coding questions, rules engine design, system design and preparation tips.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051709Z","viewCount":0},{"id":153,"title":"Visa Software Engineer 1 (SDE-1) Interview Experience | CodeSignal OA + Graph-Based Technical Rounds","url":"visa-software-engineer-1-sde-1-interview-experience-codesignal-oa-graph-based-technical-rounds","description":null,"seoDescription":" Ace your Visa SDE-1 interview with this complete interview experience covering CodeSignal OA, graph coding rounds, hiring manager questions and tips.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051788Z","viewCount":0},{"id":167,"title":"Meesho SDE-1 Online Assessment Interview Experience (Remote) - Rejected","url":"meesho-sde-1-online-assessment-interview-experience-remote-rejected","description":null,"seoDescription":"Read a real Meesho SDE-1 online assessment experience with coding questions, DSA topics, solutions, difficulty level, preparation tips and interview insights.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051805Z","viewCount":0},{"id":213,"title":"Amazon SDE II Interview Experience (Bangalore, Selected) ~ Aug 2026","url":"amazon-sde-ii-interview-experience-bangalore-selected-aug-2026","description":null,"seoDescription":"Amazon SDE II interview experience from Bangalore covering 4 rounds, DSA, LLD, system design, Leadership Principles, coding questions and selection.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051843Z","viewCount":0},{"id":172,"title":"Google L4 Software Engineer III interview experience | Selected","url":"google-l4-software-engineer-iii-interview-experience-selected","description":null,"seoDescription":"Google L4 Software Engineer III interview experience with API design, Googlyness, route matching, graph coding rounds, follow-up questions and tips.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051818Z","viewCount":0},{"id":83,"title":"Uber SDE 2 Interview Experience (5 Rounds, Selected) - Complete DSA Questions with solution, System Design & Managerial Round","url":"uber-sde-2-interview-experience","description":null,"seoDescription":"Uber SDE 2 interview experience with 5 rounds, real DSA questions, system design, coding round, and preparation tips to crack Uber interviews.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6051665Z","viewCount":0}],"latestContents":[{"id":219,"title":"6 Tools That Made My Life Easier as a Software Engineer","url":"6-tools-that-made-my-life-easier-as-a-software-engineer","description":null,"seoDescription":"The 6 developer tools I use daily as a software engineer: Git, VS Code, Docker, Postman, Chrome DevTools, and AI assistants, with real tips, mistakes, and trade","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017103Z","viewCount":0},{"id":218,"title":"When Should a Business Use Multiple AI Agents Instead of One?","url":"when-should-a-business-use-multiple-ai-agents-instead-of-one","description":null,"seoDescription":"Single agent or multi-agent AI? Learn the 4 signals that justify splitting agents by knowledge, permissions, and risk, plus routing and handoff tips.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017149Z","viewCount":0},{"id":216,"title":"AI in Clinical Trials Market Estimated to Experience a Hike in Growth by 2035","url":"ai-in-clinical-trials-market-estimated-to-experience-a-hike-in-growth-by-2035","description":null,"seoDescription":"The exclusive information about market dynamics serves as a valuable guide to predict economic scenarios and initiatives taken to enhance future growth. Our mar","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017165Z","viewCount":0},{"id":217,"title":"AI in Clinical Trials Market Estimated to Experience a Hike in Growth by 2035","url":"ai-in-clinical-trials-market-estimated-to-experience-a-hike-in-growth-by-2035","description":null,"seoDescription":"The exclusive information about market dynamics serves as a valuable guide to predict economic scenarios and initiatives taken to enhance future growth. Our mar","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017178Z","viewCount":0},{"id":215,"title":"What Happens When an AI Agent Doesn't Know the Answer?","url":"what-happens-when-an-ai-agent-doesnt-know-the-answer","description":null,"seoDescription":"Most AI agents fail quietly. They guess instead of saying \"I don't know.\" Here's how to design better fallback and human-handoff behavior.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017211Z","viewCount":0},{"id":214,"title":"Greedy Algorithms Explained: How They Work, When They Fail and How to Use Them","url":"greedy-algorithms-explained-how-they-work-when-they-fail-and-how-to-use-them","description":null,"seoDescription":"Learn greedy algorithms step by step with C# code Dijkstra, Huffman coding, knapsack and MSTs plus when greedy fails and how to prove it's correct","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017227Z","viewCount":0},{"id":213,"title":"Amazon SDE II Interview Experience (Bangalore, Selected) ~ Aug 2026","url":"amazon-sde-ii-interview-experience-bangalore-selected-aug-2026","description":null,"seoDescription":"Amazon SDE II interview experience from Bangalore covering 4 rounds, DSA, LLD, system design, Leadership Principles, coding questions and selection.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017238Z","viewCount":0},{"id":212,"title":"How to Get 10x Better AI Answers Without Writing Better Prompts : 10 Proven Techniques","url":"how-to-get-10x-better-ai-answers-without-writing-better-prompts-10-proven-techniques","description":null,"seoDescription":"Get better AI answers without complex prompts. Learn 10 practical techniques using context, examples, tools, feedback, and verification.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017253Z","viewCount":0},{"id":211,"title":"I Interviewed for a Microsoft SDE I Role. Here's Everything That Happened.","url":"i-interviewed-for-a-microsoft-sde-i-role-heres-everything-that-happened","description":null,"seoDescription":"My real Microsoft SDE I interview experience, all 4 rounds, the exact DSA problems with fully tested solutions, the WhatsApp system design round","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017267Z","viewCount":0},{"id":140,"title":"PolicyBazaar Software Development Engineer (SDE) Interview Experience | DSA, Core CS & System Design","url":"policybazaar-software-development-engineer-sde-interview-experience-dsa-core-cs-system-design","description":null,"seoDescription":"Detailed PolicyBazaar SDE interview experience covering 3 rounds: DSA coding, core CS assessment and system design with key learnings and insights.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6017282Z","viewCount":0}],"relatedContents":[{"id":7,"title":"Goldman Sachs Interview Experience – Analyst (SDE) Role, Bangalore","url":"goldman-sachs-interview-experience-analyst-sde-role-bangalore","description":null,"seoDescription":"","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159016Z","viewCount":0},{"id":84,"title":"Goldman Sachs Analyst Interview Experience (5 Rounds, Selected) | Complete DSA Questions, System Design","url":"goldman-sachs-analyst-interview-experience-5-rounds-selected-complete-dsa-questions-system-design","description":null,"seoDescription":"Goldman Sachs Analyst interview experience with 5 rounds, real DSA questions, system design, coding insights and preparation tips to get selected.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159085Z","viewCount":0},{"id":87,"title":"Goldman Sachs Analyst Internship Interview Experience | Complete Coding Questions & Real Interview Guide","url":"goldman-sachs-analyst-internship-interview-experience-complete-coding-questions-real-interview-guide","description":null,"seoDescription":"Goldman Sachs Analyst Internship interview experience for freshers: OA, coding rounds, backend questions, DSA problems and final rejection lessons.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159103Z","viewCount":0},{"id":213,"title":"Amazon SDE II Interview Experience (Bangalore, Selected) ~ Aug 2026","url":"amazon-sde-ii-interview-experience-bangalore-selected-aug-2026","description":null,"seoDescription":"Amazon SDE II interview experience from Bangalore covering 4 rounds, DSA, LLD, system design, Leadership Principles, coding questions and selection.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159119Z","viewCount":0},{"id":211,"title":"I Interviewed for a Microsoft SDE I Role. Here's Everything That Happened.","url":"i-interviewed-for-a-microsoft-sde-i-role-heres-everything-that-happened","description":null,"seoDescription":"My real Microsoft SDE I interview experience, all 4 rounds, the exact DSA problems with fully tested solutions, the WhatsApp system design round","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159171Z","viewCount":0},{"id":139,"title":"Blinkit (Eternal) SDE-1 Interview Experience (June 2026) – Offer Received","url":"blinkit-eternal-sde-1-interview-experience-june-2026-offer-received","description":null,"seoDescription":"Blinkit SDE-1 interview experience with a 30 LPA offer. Covers House Robber DP, ticket booking system design, culture fit round and tips.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159188Z","viewCount":0},{"id":140,"title":"PolicyBazaar Software Development Engineer (SDE) Interview Experience | DSA, Core CS & System Design","url":"policybazaar-software-development-engineer-sde-interview-experience-dsa-core-cs-system-design","description":null,"seoDescription":"Detailed PolicyBazaar SDE interview experience covering 3 rounds: DSA coding, core CS assessment and system design with key learnings and insights.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159206Z","viewCount":0},{"id":137,"title":"JPMorgan SDE-2 Interview Experience - 3 Rounds | Java, Code Review & System Design","url":"jpmorgan-sde-2-interview-experience-3-rounds-java-code-review-system-design","description":null,"seoDescription":" JPMorgan Chase SDE-2 interview experience covering code review, Core Java, system design, concurrency, behavioral rounds and key learnings.","contentType":5,"generatedOn":"2026-10-14T22:22:16.6159222Z","viewCount":0},{"id":135,"title":"Spinny SDE-1 Interview Experience (Selected)","url":"spinny-sde-1-interview-experience-selected","description":null,"seoDescription":"Spinny SDE-1 Interview Experience (Selected) | 4 Rounds Breakdown: Coding, LLD, Java, SQL + Real Interview Questions","contentType":5,"generatedOn":"2026-10-14T22:22:16.615924Z","viewCount":0}]}}},"source":{"isMobile":false}}; window.__CLIENT_RENDER__ = false;